Add Help for Your Program

This example shows how to provide help for the programs you write. Help text appears in the Command Window when you use the help function.

Create help text by inserting comments at the beginning of your program. If your program includes a function, position the help text immediately below the function definition line (the line with the function keyword).

For example, create a function in a file named addme.m that includes help text:

function c = addme(a,b)
% ADDME  Add two values together.
%   C = ADDME(A) adds A to itself.
%   C = ADDME(A,B) adds A and B together.
%
%   See also SUM, PLUS.

switch nargin
    case 2
        c = a + b;
    case 1
        c = a + a;
    otherwise
        c = 0;
end

When you type help addme at the command line, the help text displays in the Command Window:

addme  Add two values together.
    C = addme(A) adds A to itself.
    C = addme(A,B) adds A and B together.
 
    See also sum, plus.

The first help text line, often called the H1 line, typically includes the program name and a brief description. The Current Folder browser and the help and lookfor functions use the H1 line to display information about the program.

Create See also links by including function names at the end of your help text on a line that begins with % See also. If the function exists on the search path or in the current folder, the help command displays each of these function names as a hyperlink to its help. Otherwise, help prints the function names as they appear in the help text.

You can include hyperlinks (in the form of URLs) to Web sites in your help text. Create hyperlinks by including an HTML <a></a> anchor element. Within the anchor, use a matlab: statement to execute a web command. For example:

% For more information, see <a href="matlab: 
% web('http://www.mathworks.com')">the MathWorks Web site</a>.

End your help text with a blank line (without a %). The help system ignores any comment lines that appear after the help text block.

    Note:   When multiple programs have the same name, the help command determines which help text to display by applying the rules described in Function Precedence Order. However, if a program has the same name as a MathWorks® function, the Help on Selection option in context menus always displays documentation for the MathWorks function.

See Also

|

Related Examples

Was this topic helpful?