Skip to content

mkdocstrings-matlab

A MATLAB handler for mkdocstrings.

qualify documentation pypi version

The MATLAB handler uses Tree-sitter and its MATLAB parser to collect documentation from MATLAB source code. Via the python bindings the Abstract Syntax Tree (AST) of the source code is traversed to extract useful information. The imported objected are imported as custom Griffe objects and mocked for the python handler.

You can install this handler by specifying it as a dependency:

pyproject.toml
# PEP 621 dependencies declaration
# adapt to your dependencies manager
[project]
dependencies = [
    "mkdocstrings-matlab>=0.X.Y",
]

Preview¤

Let's us quickly see how auto-documentation works with mkdocstrings-matlab:

Function making use of Argument Validation in namespace +mynamespace
function output = typed_function(input, options)
    % Example function with typed inputs and outputs
    arguments (Input)
        input (1,1) string % The input variable
        options.keyword (1,1) double = 0 % An optional keyword argument
    end
    arguments (Output)
        output (1,:) char % The output variable
    end
    output = char(input);
end

Image title

Image title

Given the function above, the rendered documentation here is created from the following markdown document file,

docs/api.md
::: mynamespace.typed_function
    options:
      parse_arguments: true

Features¤

  • Data collection from source code: collection of the object-tree and the docstrings is done thanks to Tree-sitter.

  • Support for argument validation blocks: Tree-sitter collects your function and method argument validation blocks to display input and output argument types and default values. It is even able to automatically add cross-references o other objects from your API.

  • Recursive documentation of MATLAB namespaces: just add + to the identifer, and you get the full namespace docs. You don't need to inject documentation for each class, function, and script. Additionaly, the parent namespace documentation will be either extracted from the Contents.m or the readme.md file at the root of the namespace.

  • Support for documented properties: properties definitions followed by a docstring will be recognized in classes.

  • Multiple docstring-styles support: common support for Google-style, Numpydoc-style, and Sphinx-style docstrings. See Griffe's documentation on docstrings support.

  • Admonition support in Google docstrings: blocks like Note: or Warning: will be transformed to their admonition equivalent. We do not support nested admonitions in docstrings!

  • Every object has a TOC entry: we render a heading for each object, meaning MkDocs picks them into the Table of Contents, which is nicely displayed by the Material theme. Thanks to mkdocstrings cross-reference ability, you can reference other objects within your docstrings, with the classic Markdown syntax: [this object][namespace.subnamespace.object] or directly with [namespace.subnamespace.object][]

  • Source code display: mkdocstrings can add a collapsible div containing the highlighted source code of the MATLAB object.