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:
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.
|Function definition line
|Defines the function name, and the number and order of input and output arguments
||A one line summary description of the program, displayed when you request
||A more detailed description of the program, displayed together with the H1 line when you request
|Function or script body
||Program code that performs the actual computations and assigns values to any output arguments
||Text in the body of the program that explains the internal workings of the program
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:
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
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
or use empty square brackets:
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,
%. For the
average function, the H1 line is
This is the first line of text that appears when a user types
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
When you type
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.
Help text in an M-file can be viewed at the MATLAB command prompt only (using |
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:
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,
|Working with M-Files||Creating a Simple M-File|
© 1994-2005 The MathWorks, Inc.