2.10: Script Documentation
- Page ID
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.