MATLAB Programming
MATLAB Programming MATLAB Programming
4 M-File Programming Function Definition Line The function definition line informs MATLAB that the M-file contains a function, and specifies the argument calling sequence of the function. The function definition line for the fact function is All MATLAB functions have a function definition line that follows this pattern. Function Name. Function names must begin with a letter, may contain any alphanumeric characters or underscores, and must be no longer than the maximum allowed length (returned by the function namelengthmax). Because variables must obey similar rules, you can use the isvarname function to check whether a function name is valid: isvarname myfun Although function names can be of any length, MATLAB uses only the first N characters of the name (where N is the number returned by the function namelengthmax) and ignores the rest. Hence, it is important to make each function name unique in the first N characters: N = namelengthmax N = 63 Note Some operating systems may restrict file names to shorter lengths. The name of the text file that contains a MATLAB function consists of the function name with the extension .m appended. For example, average.m 4-10
Working with M-Files If the filename and the function definition line name are different, the internal (function) name is ignored. Thus, if average.m is the file that defines a function named computeAverage, you would invoke the function by typing average Note While the function name specified on the function definition line does not have to be the same as the filename, it is best to use the same name for both to avoid confusion. Function Arguments. If the function has multiple output values, enclose the output argument list in square brackets. Input arguments, if present, are enclosed in parentheses following the function name. Use commas to separate multiple input or output arguments. Here is the declaration for a function named sphere that has three inputs and three outputs: function [x, y, z] = sphere(theta, phi, rho) Ifthereisnooutput,leavetheoutputblank function printresults(x) or use empty square brackets: function [] = printresults(x) The variables that you pass to the function do not need to have the same name as those in the function definition line. The H1 Line The H1 line, so named because it is the first help text line, is a comment line immediately following the function definition line. Because it consists of comment text, the H1 line begins with a percent sign, %. Fortheaverage function, the H1 line is % AVERAGE Mean of vector elements. 4-11
- Page 292 and 293: 3 Basic Program Components statemen
- Page 294 and 295: 3 Basic Program Components Characte
- Page 296 and 297: 3 Basic Program Components Quantifi
- Page 298 and 299: 3 Basic Program Components Named To
- Page 300 and 301: 3 Basic Program Components Comma-Se
- Page 302 and 303: 3 Basic Program Components 40 Thisi
- Page 304 and 305: 3 Basic Program Components ans = 10
- Page 306 and 307: 3 Basic Program Components X = -pi:
- Page 308 and 309: 3 Basic Program Components Program
- Page 310 and 311: 3 Basic Program Components if A S1
- Page 312 and 313: 3 Basic Program Components end disp
- Page 314 and 315: 3 Basic Program Components while St
- Page 316 and 317: 3 Basic Program Components Program
- Page 318 and 319: 3 Basic Program Components Asterisk
- Page 320 and 321: 3 Basic Program Components Numeric
- Page 322 and 323: 3 Basic Program Components Cell Arr
- Page 324 and 325: 3 Basic Program Components Dynamic
- Page 326 and 327: 3 Basic Program Components The purp
- Page 328 and 329: 3 Basic Program Components Function
- Page 330 and 331: 3 Basic Program Components MATLAB F
- Page 332 and 333: 3 Basic Program Components Forcing
- Page 334 and 335: 4 M-File Programming Function Argum
- Page 336 and 337: 4 M-File Programming Saving the Pro
- Page 338 and 339: 4 M-File Programming Improving Perf
- Page 340 and 341: 4 M-File Programming Working with M
- Page 344 and 345: 4 M-File Programming This is the fi
- Page 346 and 347: 4 M-File Programming The process lo
- Page 348 and 349: 4 M-File Programming Providing Help
- Page 350 and 351: 4 M-File Programming M-File Scripts
- Page 352 and 353: 4 M-File Programming Simple Functio
- Page 354 and 355: 4 M-File Programming 3 Type inmem t
- Page 356 and 357: 4 M-File Programming sqr = @(x) x.^
- Page 358 and 359: 4 M-File Programming Call plotFhand
- Page 360 and 361: 4 M-File Programming This false res
- Page 362 and 363: 4 M-File Programming Create two equ
- Page 364 and 365: 4 M-File Programming • Overloadin
- Page 366 and 367: 4 M-File Programming Here is a more
- Page 368 and 369: 4 M-File Programming axis([xmin fix
- Page 370 and 371: 4 M-File Programming This section c
- Page 372 and 373: 4 M-File Programming addOptional(na
- Page 374 and 375: 4 M-File Programming % Display the
- Page 376 and 377: 4 M-File Programming Arguments That
- Page 378 and 379: 4 M-File Programming Enabling Case-
- Page 380 and 381: 4 M-File Programming Property Funct
- Page 382 and 383: 4 M-File Programming Using nargin a
- Page 384 and 385: 4 M-File Programming 4-52
- Page 386 and 387: 4 M-File Programming Determining Wh
- Page 388 and 389: 4 M-File Programming 7 Function els
- Page 390 and 391: 4 M-File Programming While the comm
Working with M-Files<br />
If the filename and the function definition line name are different, the<br />
internal (function) name is ignored. Thus, if average.m is the file that defines<br />
a function named computeAverage, you would invoke the function by typing<br />
average<br />
Note While the function name specified on the function definition line does<br />
not have to be the same as the filename, it is best to use the same name for<br />
both to avoid confusion.<br />
Function Arguments. If the function has multiple output values, enclose<br />
the output argument list in square brackets. Input arguments, if present, are<br />
enclosed in parentheses following the function name. Use commas to separate<br />
multiple input or output arguments. Here is the declaration for a function<br />
named sphere that has three inputs and three outputs:<br />
function [x, y, z] = sphere(theta, phi, rho)<br />
Ifthereisnooutput,leavetheoutputblank<br />
function printresults(x)<br />
or use empty square brackets:<br />
function [] = printresults(x)<br />
The variables that you pass to the function do not need to have the same<br />
name as those in the function definition line.<br />
The H1 Line<br />
The H1 line, so named because it is the first help text line, is a comment<br />
line immediately following the function definition line. Because it consists<br />
of comment text, the H1 line begins with a percent sign, %. Fortheaverage<br />
function, the H1 line is<br />
% AVERAGE Mean of vector elements.<br />
4-11