'Sphinx with both Python and Matlab (sphinxcontrib-matlabdomain): Import fail

I have a project including a Python package as well as some Matlab code, which should all go into the same documentation created by Sphinx. The Python part of the documentation works flawlessly quite some time already - the Matlab part was added now and makes some trouble.

The data structure more or less is as follows:

|- python
     |- modules
     |- including subpackages
                            |- some sub-modules
|- matlab
      |- scripts
      |- functions
|- docs
      |- source
              |- conf.py
              |- matlab.rst
              |- python.rst
              |- index.rst
              |- ...

The most relevant lines in conf.py are prob. the following:

import os
import sys
# for the Python autodoc package
sys.path.insert(0, os.path.abspath('../..'))
extensions = ['sphinx.ext.napoleon', 'sphinxcontrib.matlab', 'sphinx.ext.autodoc']
# Path to the Matlab files
matlab_src_dir = '../../matlab'
#matlab_src_dir = '<<abssolute_path>>/matlab'

As you see, I tried both relative and absolute paths for matlab_src_dir. So far, without any difference.

The python.rst, which was created automatically by apidoc, contains:

python package
==============

.. automodule:: python
   :members:
   :undoc-members:
   :show-inheritance:

Subpackages
-----------

.. toctree::
   :maxdepth: 4

   python.subpackages

The matlab.rst was created manually and contains:

matlab package
==============

.. mat:automodule:: matlab
   :members:
   :undoc-members:
   :show-inheritance:

Subscripts
----------

.. toctree::
   :maxdepth: 4

   matlab.function_file

Thus, python.rst and matlab.rst are more or less the same but with mat:automodule for Matlab according to the documentation.

Finally, index.rst contains:

.. toctree::
   :maxdepth: 2
   
   python

.. toctree::
   :maxdepth: 2
   
   matlab

Now, when I run Sphinx' make html I receive the following error, which results in a nice Python documentation but an empty Matlab documentation:

WARNING: [sphinxcontrib-matlabdomain]: failed to import module 'matlab'; the following exception was raised:
Traceback (most recent call last):
  File "C:\Users\<<username>>\AppData\Roaming\Python\Python39\site-packages\sphinxcontrib\mat_documenters.py", line 117, in import_object
    obj = self.module = modules[self.modname]
KeyError: 'matlab'

What am I missing here. Why can't Sphinx find the matlab folder with the containing m-files?

And if that should be solved, maybe a secondary question: is there any similar function as apidoc for Matlab files, so that I do not need to create all *.rst files myself?

Thank you for any hints!

BTW: I am running on Sphinx v4.4.0 under Python 3.9.7.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source