How to run matlab script in python when the script lives inside different folder?

479 views Asked by At

I am trying to run matlab script, on python jupyter notebook, using matlab python engine, thus I have three files:

  1. matlab script.m
  2. python wraper.py
  3. jupyter notebook.ipynb

I can run it properly when I put all the tree files in the same folder, but what I want to do is run the jupyter notebook with this folder structure:

├── matlab                    
│   ├── matlabfunction.m       # 1         
│   └── pythonwraper.py        # 2    
├── jupyternotebook.ipynb      # 3

However, when I use the structure above, I get this error:

matlab.engine.MatlabExecutionError: Undefined function 'matlabfunction' for input arguments of type 'char'.

Please note that I call the matlab script inside pythonwraper.py like this:

import matlab
import matlab.engine
m_eng = matlab.engine.start_matlab()
m_eng.matlabfunction(some_parameters)

I would like to know what is the cause of the error and how can I fix it?

1

There are 1 answers

0
mhopeng On

You need to add your new "matlab" folder to the path that is available to the Matlab Engine. You can use the standard Matlab path commands to add your new folder to the path, like this:

m_eng.addpath('matlab')