This guide provides step-by-step instructions for installing and configuring Doxygen, Perl, and other necessary tools to generate documentation for MATLAB projects on both Windows and macOS systems.
Ensure you have the following:
- A computer running Windows or macOS
- Administrator privileges
- MATLAB source files for documentation
- Download the latest version of Doxygen from Doxygen's official website.
- Run the installer and follow the setup instructions.
- During installation, ensure the option "Add doxygen to the system PATH for all users" is selected.
To ensure that Doxygen is accessible from the command line on Windows, follow these steps to add its installation directory to your system's PATH environment variable.
- By default, Doxygen is installed in:
C:\Program Files\doxygen\bin - Verify that this directory contains the executable file
doxygen.exe.
- Press Win + R, type
control, and press Enter to open the Control Panel. - Navigate to:
- System and Security > System > Advanced system settings.
- In the Advanced tab, click on Environment Variables.
- Under System variables, locate the Path variable and click Edit.
- Click New and add the following path:
C:\Program Files\doxygen\bin - Click OK to save the changes and close all dialogs.
- Close and reopen the Command Prompt.
- Run the following command to verify the installation:
or
doxygen --versionDoxygen --version - If the installation is successful, the installed version of Doxygen will be displayed.
- Install Homebrew if not already installed:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - Use Homebrew to install Doxygen:
brew install doxygen
- Verify the installation:
doxygen --version
- Download and install Strawberry Perl from Strawberry Perl's website.
- Ensure that the Perl binary is added to your system PATH during installation (same as for Doxygen).
- Find the Perl path by running:
where perl - Verify the installation:
perl --version
- Perl is pre-installed on macOS. Verify the installation:
perl --version
- Find the Perl path by running:
which perl
- Download and install Graphviz from Graphviz's website.
- During installation, ensure "Add Graphviz to the system PATH" is selected.
- Find the Graphviz
dotpath by running:where dot - Verify the installation:
dot -version
- Use Homebrew to install Graphviz:
brew install graphviz
- Find the Graphviz
dotpath by running:which dot
- Verify the installation:
dot -version
- Generate a default
Doxyfile:doxygen -g
- Open the generated
Doxyfilein a text editor and make the following modifications:- Set the project name:
PROJECT_NAME = "MATLAB Documentation" - Define the input directory where your MATLAB files are located:
INPUT = path/to/your/matlab/project - Enable recursive search for subdirectories:
RECURSIVE = YES - Specify the input file patterns:
FILE_PATTERNS = *.m - Set the input filter to use the
m2cpp.plscript:FILTER_PATTERNS = *.m=path/to/m2cpp.pl - Enable class diagrams and graphs using Graphviz:
HAVE_DOT = YES DOT_PATH = path/to/graphviz/bin - Modify the following settings to include more detailed documentation (only if lines are present):
EXTRACT_ALL = YES EXTRACT_STATIC = YES EXTRACT_LOCAL_CLASSES = YES HIDE_UNDOC_MEMBERS = NO HIDE_UNDOC_CLASSES = NO
- Set the project name:
-
Open the
m2cpp.plscript in a text editor. -
Modify the shebang line at the top of the file to point to the Perl executable:
#! [path_of_perl(look_above)]example: #!/usr/local/bin/perl
#![path_of_perl(look_above)]example: #!/usr/local/bin/perl
-
Save and close the file.
- Run Doxygen with the updated
Doxyfile:doxygen Doxyfile
- The documentation will be generated in the output directory specified in the
Doxyfile(default is./html).
- Navigate to the output directory.
- Open
index.htmlin a browser to view the generated documentation. - Ensure that:
- Functions and classes are documented.
- Graphs and diagrams are included if applicable.
To ensure your MATLAB files are properly documented, follow these guidelines:
Below is a table describing how to structure comments in MATLAB files to ensure compatibility with the m2cpp.pl script and Doxygen.
| Code Element | Comment Structure | Example |
|---|---|---|
| File | Use @file for the filename and @brief for a short description. |
matlab %> @file FileName.m %> @brief Short description of the file. %> Additional details if necessary. |
| Class | Add @brief above the class declaration, explaining its purpose. |
matlab %> @brief Description of the class. %> Additional details about its functionality. classdef MyClass |
| Public Properties | Use %> above the property to describe it. |
matlab properties (Access = public) %> Description of the public property publicProperty end |
| Constant Properties | Specify the property as constant and describe it with %>. |
matlab properties (Constant = true) %> Description of a constant property constantProperty = 42; end |
| Method | Use @brief for a short description, @param for parameters, and @retval for return values. |
matlab %> @brief Short description of the method. %> Detailed explanation. %> @param input Description of the input parameter. %> @retval output Description of the output value. function output = exampleMethod(obj, input) end |
| Enumerations | Use %> above each enumeration element for its description. |
matlab enumeration %> Description of the first element FirstElement %> Description of the second element SecondElement end |
| Events | Use %> to describe each event declared in the class. |
matlab events %> Description of the first event FirstEvent %> Description of the second event SecondEvent end |
| Static/Protected Methods | Indicate the accessibility of the method and use @brief, @param, and @retval as for regular methods. |
matlab methods (Static, Access = protected) %> @brief Static and protected method. %> Detailed explanation. %> @param input Description of the parameter. %> @retval output Return value. function output = exampleStaticMethod(input) end end |
| Abstract Methods | Add a description to the abstract method and specify parameters and return values. | matlab methods (Abstract = true) %> @brief Abstract method. %> Only the signature is declared. %> @param input Description of the parameter. %> @retval output Return value. output = abstractMethod(input); end |
- Consistency: Always follow a consistent commenting style across all MATLAB files.
- Clarity: Ensure comments are clear, concise, and professional.
- Mandatory Fields: Always include
@brief, and when applicable, use@param,@retval, and%>.
classdef MyClass
properties
data
end
methods
function obj = MyClass(inputData)
obj.data = inputData;
end
end
end%> @file MyClass.m
%> @brief Example class demonstrating documentation style.
%> This class serves as a container for data.
classdef MyClass
properties
%> Stored data in the class
data
end
methods
% ======================================================================
%> @brief Constructor for the MyClass class.
%> Initializes an instance of the class with the provided data.
%>
%> @param inputData Input data for initialization.
%> @retval obj Instantiated object of the MyClass class.
% ======================================================================
function obj = MyClass(inputData)
obj.data = inputData;
end
end
endInstruction:
You are an expert assistant in documenting MATLAB code, specializing in comments compatible with Doxygen and the m2cpp.pl script. Given the following piece of MATLAB code, you must rewrite it entirely, ensuring that all comments follow the guidelines below.
Commenting Guidelines:
-
File Comments: At the beginning of the file, include:
@filefollowed by the filename (e.g.,MyClass.m)@brieffor a short description of the file.
-
Class Comments: Directly above the
classdefline, use@briefto describe the class, its purpose, and main details. -
Property Comments: For each property, add
%>comments above the property definition.- Indicate the property scope (public, private, protected, or constant).
- Provide a brief description of the property.
-
Method Comments: For each method, add
%>comments before its definition:@briefto describe what the method does.@paramfor each input parameter.@retvalfor returned values.
-
Enumeration Comments: For enumerated values, place
%>comments above each enum element with a brief description. -
Event Comments: For events, add
%>comments above the event declaration, describing it.
General Rules:
- Always rewrite the given MATLAB code with the updated comments.
- Preserve the original structure and logic.
- If the original code lacks comments, create them based on the function or property’s purpose.
- Ensure the comments are clear, professional, and follow Doxygen-style documentation.
Input Code: [PASTE YOUR MATLAB CODE HERE]
Output: You should return the fully documented MATLAB code, with every class, file header, property, method, enumeration, and event annotated as described above.
- Doxygen cannot find the
dotexecutable: Ensure Graphviz is correctly installed and added to the system PATH. - Perl script errors: Verify the correct path to the Perl interpreter in
m2cpp.pl. - Missing documentation for functions: Ensure functions are properly formatted in MATLAB files and follow Doxygen's syntax for comments.
- Old guide for Doxygen for MATLAB
- Doxygen Documentation
- Graphviz Documentation
- Strawberry Perl Documentation
This project utilizes the m2cpp.pl script from the package "Using Doxygen with Matlab" by Fabrice, available on MATLAB Central File Exchange.
Cite as: Fabrice (2024). Using Doxygen with Matlab (https://www.mathworks.com/matlabcentral/fileexchange/25925-using-doxygen-with-matlab), MATLAB Central File Exchange. Retrieved December 13, 2024.