Programming |
Basic Parts of an M-File
This simple function shows the basic parts of an M-file. Note that any line that begins with %
is not executable:
function f = fact(n) Function definition line % Compute a factorial value. H1 line % FACT(N) returns the factorial of N, Help text % usually denoted by N! % Put simply, FACT(N) is PROD(1:N). Comment f = prod(1:n); Function body
The table below briefly describes each of these M-file parts. Both functions and scripts can have all of these parts, except for the function definition line which applies to functions only. These parts are described in greater detail following the table.
M-File Element |
Description |
Function definition line (functions only) |
Defines the function name, and the number and order of input and output arguments |
H1 line |
A one line summary description of the program, displayed when you request help on an entire directory, or when you use lookfor |
Help text |
A more detailed description of the program, displayed together with the H1 line when you request help on a specific function |
Function or script body |
Program code that performs the actual computations and assigns values to any output arguments |
Comments |
Text in the body of the program that explains the internal workings of the program |
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:
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:
The name of the text file that contains a MATLAB function consists of the function name with the extension .m
appended. For example,
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
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:
If there is no output, leave the output blank
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, 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, %
. For the average
function, the H1 line is
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.
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.
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:
[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
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,
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.
|
Working with M-Files | Creating a Simple M-File |
© 1994-2005 The MathWorks, Inc.