Search code examples
pythonmatlabpython-sphinx

Sphinx with Matlab does not generate documentation


I have important MATLAB libraries and I want to document them through Sphinx. The data structure more or less is as follows

|- Matlab
      |- scripts
      |- functions
|- docs
      |- source
      |- conf.py
      |- index.rst
      |- Makefile
      |- make.bat
              |- ...

Relevant lines in conf.py are

import os

extensions = [
'sphinx.ext.viewcode', 'sphinx.ext.autodoc', 'sphinxcontrib.matlab'
]

this_dir = os.path.dirname(os.path.abspath('Matlab'))
matlab_src_dir = os.path.abspath(os.path.join(this_dir, '..'))
primary_domain = 'mat'

When running make html from /docs? I don't get any errors but no documentation is generated.

logs

Running Sphinx v5.0.2
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 0 source files that are out of date
updating environment: 0 added, 0 changed, 0 removed
looking for now-outdated files... none found
no targets are out of date.
build succeeded.

The HTML pages are in _build\html.

Solution

  • In your index.rst file (or other .rst file that index.rst points to), you'll need a directive for it to key off of. Something like this may work in your index.rst:

    functions
    =========
    
    .. mat:automodule:: functions
        :members:
        :show-inheritance:
        :undoc-members:
    

    I highly recommend looking at the code examples in the matlabdomain github.

    It also looks like your matlab source dir is off a little. Based on your structure I would assume it'd be this:

    matlab_src_dir = os.path.abspath(os.path.join('..', 'Matlab'))
    

    And one more thing; if you want to pull the comments out of your code headers, you also need to run the autodoc at some point. The sphinx quickstart will put that execution in the make.bat file, or you can run it manually. I have a python script that runs it all (rather than make). However the call is:

    sphinx-apidoc -f -M -o {doc_dir} {code_dir}
    

    where for you (if running at your base dir) {doc_dir} is ./docs and {code_dir} would be ./Matlab