MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

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

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!