>> 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.