MATLAB documentation
Code comments
MATLAB has published a set of Coding Guidelines in 2025, which includes a section on code comments. Code comments are an essential part of code documentation, as they help explain the purpose and functionality of the code to other developers (and your future self).
Docstrings
Docstrings are structured comments, associated with segments (rather than lines) of code which can be used to generate user documentation for users of your project. If properly formated, docstrings can be automatically extracted and compiled into user-friendly formats such as HTML or PDF, and can be accessed through the help function in MATLAB.
Unfortunately, MATLAB does not offer a defined standard for docstring formatting, but the following structure is used by MATLAB for internal functions.
Example docstring format
function output = myFunction(input)
%myFunction - This function does something useful.
% Detailed explanation goes here
%
% Syntax
% output = myFunction(input)
%
% Inputs Arguments
% input - Description of input argument
% data type
%
% Output Arguments
% output - Description of output argument
% data type
%
% Examples
% Example usage of the functionThe documentation can then be accessed in MATLAB through the help command:
help myFunctionor viewed in a separate window with doc:
doc myFunctionCompatibility with Sphinx
If your software project uses Sphinx for documentation generation, you can use the sphinxcontrib-matlabdomain extension to parse MATLAB docstrings and include them in your Sphinx-generated documentation. For more information on how to set up and use this extension, refer to our Tooling section.