MATLAB Programming
MATLAB Programming MATLAB Programming
4 M-File Programming This is the first line of text that appears when a user types help functionname at the MATLAB prompt. Further, the lookfor function searches on and displays only the H1 line. Because this line provides important summary information about the M-file, it is important to make it as descriptive as possible. Help Text You can create online help for your M-files by entering help text on one or more consecutive comment lines at the start of your M-file program. MATLAB considers the first group of consecutive lines immediately following the H1 line that begin with % to be the online help text for the function. The first line without % as the left-most character ends the help. The help text for the average function is % AVERAGE(X), where X is a vector, is the mean of vector % elements. Nonvector input results in an error. When you type help functionname at the command prompt, MATLAB displays the H1 line followed by the online help text for that function. The help system ignores any comment lines that appear after this help block. Note Help text in an M-file can be viewed at the MATLAB command prompt only (using help functionname). You cannot display this text using the MATLAB Help browser. You can, however, use the Help browser to get help on MATLAB functions and also to read the documentation on any MathWorks products. The Function or Script Body The function body contains all the MATLAB code that performs computations and assigns values to output arguments. The statements in the function body can consist of function calls, programming constructs like flow control and interactive input/output, calculations, assignments, comments, and blank lines. For example, the body of the average function contains a number of simple programming statements: 4-12
Working with M-Files [m,n] = size(x); if (~((m == 1) | (n == 1)) | (m == 1 & n == 1)) % Flow control error('Input must be a vector') % Error message display end y = sum(x)/length(x); % Computation and assignment Comments As mentioned earlier, comment lines begin with a percent sign (%). Comment lines can appear anywhere in an M-file, and you can append comments to the end of a line of code. For example, % Add up all the vector elements. y = sum(x) % Use the sum function. In addition to comment lines, you can insert blank lines anywhere in an M-file. Blank lines are ignored. However, a blank line can indicate the end of the help text entry for an M-file. Block Comments. To write comments that require more than one line, use the block comment operators, %{ and %}: %{ This next block of code checks the number of inputs passed in, makes sure that each input is a valid data type, and then branches to start processing the data. %} Note The %{ and %} operators must appear alone on the lines that immediately precede and follow the block of help text. Do not include any other text on these lines. Creating a Simple M-File You create M-files using a text editor. MATLAB provides a built-in editor, but you can use any text editor you like. Once you have written and saved the M-file, you can run the program as you would any other MATLAB function or command. 4-13
- 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 342 and 343: 4 M-File Programming Function Defin
- 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
- Page 392 and 393: 4 M-File Programming strcmp str1 st
Working with M-Files<br />
[m,n] = size(x);<br />
if (~((m == 1) | (n == 1)) | (m == 1 & n == 1)) % Flow control<br />
error('Input must be a vector') % Error message display<br />
end<br />
y = sum(x)/length(x);<br />
% Computation and assignment<br />
Comments<br />
As mentioned earlier, comment lines begin with a percent sign (%). Comment<br />
lines can appear anywhere in an M-file, and you can append comments to the<br />
end of a line of code. For example,<br />
% Add up all the vector elements.<br />
y = sum(x)<br />
% Use the sum function.<br />
In addition to comment lines, you can insert blank lines anywhere in an<br />
M-file. Blank lines are ignored. However, a blank line can indicate the end<br />
of the help text entry for an M-file.<br />
Block Comments. To write comments that require more than one line, use<br />
the block comment operators, %{ and %}:<br />
%{<br />
This next block of code checks the number of inputs<br />
passed in, makes sure that each input is a valid data<br />
type, and then branches to start processing the data.<br />
%}<br />
Note The %{ and %} operators must appear alone on the lines that<br />
immediately precede and follow the block of help text. Do not include any<br />
other text on these lines.<br />
Creating a Simple M-File<br />
You create M-files using a text editor. <strong>MATLAB</strong> provides a built-in editor, but<br />
you can use any text editor you like. Once you have written and saved the<br />
M-file, you can run the program as you would any other <strong>MATLAB</strong> function<br />
or command.<br />
4-13