Skip to main content
Engineering LibreTexts

11: Script Documentation

  • Page ID
    85945
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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 11: Script Documentation 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; a detailed edit history is available upon request.