Consider a simple function that sums two scalar numeric values:
function R = mysum(x,y) R = x + y; end
A function is incomplete if it does not state clearly how to use it. The help documentation of a function should include:
- The purpose of the function
- A description of each supported syntax
- Examples on how to use the function
- Links to related resources
- Any other relevant information
Use a uniform style to document your functions. I recommend that you follow the style used by The MathWorks to document its own functions.
- The first line includes the name of the function and a brief description
- The Examples section begins with
- Specify the name of related functions in a line that begins with
% See also
The best way to gain skills in this matter is inspecting the source code of the functions available in the MATLAB installation. To do so, use the functions
% Show the file 'imread.m' in the Command Window type imread;
% Open the file 'imread.m' in the Editor edit imread;
I recommend the following books to learn more on functions and the help system in MATLAB.
This book was the winner of a Textbook Excellence Award. The sixth chapter covers user-defined functions, scope of variables, debugging, and other aspects related to the development of MATLAB programs.
MATLAB for Engineers (5th Edition)
This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. The third chapter explains the help feature, and the sixth chapter covers various topics related to functions.
I also recommend the other tutorials in this series.
- How to Define Functions in MATLAB
- How to Call a Function in MATLAB
- How to Add Help Documentation to a Function in MATLAB
- Local Functions in MATLAB
- Nested Functions in MATLAB
- Visibility of Nested Functions in MATLAB
- Private Functions in MATLAB
- Anonymous Functions in MATLAB
- Inside the Handle of Anonymous Functions in MATLAB
- Functions with Variable Input Arguments in MATLAB
- Functions with Variable Output Arguments in MATLAB
- How to Validate the Number of Input Arguments Passed to a Function in MATLAB
- How to Validate the Number of Output Arguments Passed to a Function in MATLAB
I hope you have learned how to add help documentation to a function in MATLAB. The source code developed in this tutorial is available at this page.