2.11: Script Documentation

Matlab and Octave Programming for STEM Applications · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    2.11: Script Documentation

    Every script should provide documentation, which is a comment that explains what the script does and what its requirements are.

    For example, I might put something like this at the beginning of fibonacci1.m:

    % Computes a numerical approximation of the nth Fibonacci number.
    % Precondition: you must assign a value to n before running this script.
    % Postcondition: the result is stored in ans.

    A precondition is something that must be true when the script starts in order for it to work correctly. A postcondition is something that will be true when the script completes.

    If there is a comment at the beginning of a script, MATLAB assumes it’s the documentation for the script. So if you type help fibonacci1, you get the contents of the comment (without the percent signs).

    >> help fibonacci1
      Computes a numerical approximation of the nth Fibonacci number.
      Precondition: you must assign a value to n before running this script.
      Postcondition: the result is stored in ans.

    That way, scripts that you write behave just like predefined scripts. You can even use the doc command to see your comment in the Help Window.


    This page titled 2.11: Script Documentation was last modified on Wed, 13 Jul 2022 23:32:32 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform.