Understanding M-Files in MATLAB

M-Files
M-Files in MATLAB can be simple source files that execute a series of MATLAB statements or functions that receive inputs from variables and produce one or more outputs. This article focuses on M-File functions. These functions extend the capabilities of MATLAB and IPT to access specific user-defined applications.
M-Files are created using a text editor and are stored with filenames in the form of filename.m, such as average.m and filter.m. The components of an M-File are as follows:
* Function definition line
* H1 statement
* Help text
* Function body
* Commands
The function definition line takes the form:
function [outputs] = name(inputs)
For example, a function that calculates the sum and product of two images (two different outputs) should have the following form:
function [s, p] = sumprod(f, g)
Here, f and g are the input images, s is the summed image, and p is the product image. The name sumprod can be defined arbitrarily (as long as it conforms to the constraints mentioned at the end of this section), but the keyword function always appears on the left side. Note that output parameters must be enclosed in square brackets, while input parameters are enclosed in parentheses. If the function has only a single output parameter, you can list it directly without brackets. If the function has no outputs, just use function without parentheses or equals sign. The function name must start with a letter, followed by any combination of letters, numbers, or underscores, but spaces are not allowed. MATLAB can recognize function names up to 63 characters long; any characters beyond that will be ignored.
Functions can be called from the command prompt, for example:
>> [s, p] = sumprod(f, g);
They can also be used as elements of other functions, in which case those functions become subfunctions. As mentioned earlier, if there is only a single output variable, you can also omit the parentheses, for example:

>> y = sum(x);

The H1 statement is the first line of text, which is a separate comment line immediately following the function definition line. There should be no empty lines or spaces between the function definition line and the H1 statement. An example of an H1 statement is as follows:

SUMPROD Computes the sum and product of two images.

When the user types at the MATLAB prompt

>> help function_name

the H1 statement appears first. Typing lookfor keyword will display all H1 statements containing the string keyword. This provides important summary information about the M-File, so it should be as descriptive as possible.

Help text is the block of text immediately following the H1 statement, with no empty lines between them. The help text is used to provide comments or online help for the function. When the user types help function_name at the prompt, MATLAB displays all comment lines between the function definition line and the first non-comment line (execution statement or blank statement). However, the help system ignores any comment lines after the help text block.

The function body contains all the MATLAB code that performs calculations and assigns values to output variables. Some examples of MATLAB code will be listed later in this chapter.

Lines following the symbol “%” that are not H1 statements or help text are considered comment lines for the function; they are not part of the help text. Comments can be added at the end of code lines.

M-Files can be created and edited in any text editor and saved with the extension .m in a specified directory, usually in the MATLAB search path. Another way to create and edit M-Files is to use the edit function at the prompt. For example, if the file exists in the directory of the MATLAB search path or in the current directory, you can type:

>> edit sumprod

This will open the file sumprod.m for editing. If the file is not found, MATLAB will provide the user with options to create the file. The MATLAB editing window has many drop-down menus that can perform tasks such as saving files, checking files, and debugging files. Text editors can perform some simple checks and use different colors to distinguish various code elements, so it is recommended to use a text editor when writing or editing M-Files.

End

Leave a Comment