Skip to Main Content Skip to Search
Product Documentation

Add Help for Your Program Files

Help Within a Program File

Help consists of lines of comments at the beginning of a program file. Help that you add to your file displays like the help for MATLAB functions. The first help text line, often called the H1 line, includes the function name and a brief description.

For example, here is an excerpt from the file hist.m:

function [no,xo] = hist(varargin)
%HIST  Histogram.
%   N = HIST(Y) bins the elements of Y into 10 equally spaced containers
%   and returns the number of elements in each container.  If Y is a
%   matrix, HIST works down the columns.
%
%   N = HIST(Y,M), where M is a scalar, uses M bins.
%
%   N = HIST(Y,X), where X is a vector, returns the distribution of Y
%   among bins with centers specified by X. The first bin includes
%   data between -inf and the first center and the last bin
%   includes data between the last bin and inf. Note: Use HISTC if
%   it is more natural to specify bin edges instead. 
%
%   Class support for inputs Y, X: 
%      float: double, single
%
%   See also HISTC, MODE.

When you type help hist, the help text displays in the Command Window:

 hist  Histogram.
    N = hist(Y) bins the elements of Y into 10 equally spaced containers
    and returns the number of elements in each container.  If Y is a
    matrix, hist works down the columns.
 
    N = hist(Y,M), where M is a scalar, uses M bins.
 
    N = hist(Y,X), where X is a vector, returns the distribution of Y
    among bins with centers specified by X. The first bin includes
    data between -inf and the first center and the last bin
    includes data between the last bin and inf. Note: Use HISTC if
    it is more natural to specify bin edges instead. 
 
    Class support for inputs Y, X: 
       float: double, single
 
    See also histc, mode.

    Overloaded methods:
       fints/hist
       categorical/hist

    Reference page in Help browser
       doc hist

Create See also links by including function names at the end of your help text on a line that begins with % See also. The list of names can include MATLAB functions, toolbox functions, and your own functions. 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, MATLAB prints the function names as they appear in the help text.

End your help text with a blank line (without a %).

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

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

When you are connected to the Internet and click the link, MATLAB opens a Web browser window to display the URL. For more information, see Display Hyperlinks in the Command Window.

For information about formatting help in the Editor, see Add Comments.

For related information, see Generating a Summary View of the Help Components in Functions and Scripts.

Help Summary for Your Program Files (Contents.m)

Provide a summary file for your own collection of program files using the same method as MATLAB. In MATLAB, each folder containing program files includes a file named Contents.m (with a capital C) that lists the functions in the folder with a brief description of each. You can view the information in a Contents.m file from the command line in several ways:

To create your own Contents.m files:

The Upslope Area toolbox example folder contains a Contents.m file. For more information, see Learning to Add Help from Examples.

You can create a categorical listings of functions for the Help browser by marking up your Contents.m file and publishing it to HTML. To learn more, see Creating Function and Block Category Listings.

Help for Classes You Create

If you create your own MATLAB classes, you can provide help for the class by including comments in the class definition file:

List the properties and methods of the class within the first block of comments after classdef. If you format the list in as described here, MATLAB renders the property and method names you list as hyperlinks to their definitions, which appear later in the same file:

  1. To create links from this section to your class properties, add a line:

    % Classname Properties:

    where Classname is the name from your classdef. Be sure to put a colon (:) after Properties.

  2. List your property names (with optional same-line descriptions) on the following lines. For example:

      %   prop1 - first property
      %   prop2 - second property
    
  3. List the methods; enter the class name followed by Methods: (include a colon). Then, list your methods (with optional same-line descriptions) on the following lines, as follows:

    % MyClass Methods:
    %   method1 - first method
    %   method2 - second method
    

View help for your class in the Command Window:

help classname

To view the same help for the class in the Help browser, run:

doc classname

For more information about getting help for classes, read the following section. To learn more about how to create class definitions, see User-Defined Classes.

Example of Help for a Externally Supplied Class

The following example shows help for a class file, sads.m, an example provided with MATLAB documentation. If you create help for your class files, the help should look and work like this example.

Follow these steps to see the help for the example.

  1. Make sure you can access the examples folder by either:

    • Making the folder containing the example file as your current folder:

      cd(fullfile(matlabroot,'help','techdoc','matlab_env', 'examples'))
      
    • Adding the examples folder to the search path:

      addpath(fullfile(matlabroot,'help','techdoc','matlab_env', 'examples'))
      

      Or, Click here to add the help examples folder to the search path.

  2. Open the class file in the Editor to see the help comments.

    open('sads.m')
  3. View help for the sads class in the Help browser:

    doc sads

  4. Access more information by following links on the class help page or by using the doc function. For example, to get help for the steer method, do one of the following:

    • Click the steer link under Method Summary.

    • Run doc sads.steer.

    You also can open the sads.m file by clicking the View code for sads link at the top of the sads help page, as described previously.

  5. Next, view information about sads objects another way. Create an instance of sads, for example, sensorArray, and then open that object in the Variable Editor:

    loadparameters
    sensorArray=sads(Data, Wavelength,SampleRate,Spacing,Name);
    openvar sensorArray  % or double-click sensorArray in the Workspace browser

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS