Skip to content

Instantly share code, notes, and snippets.

@rahulbhadani
Created April 7, 2020 20:32
Show Gist options
  • Save rahulbhadani/8af690a4e22e5b618c33b10e3adedbc8 to your computer and use it in GitHub Desktop.
Save rahulbhadani/8af690a4e22e5b618c33b10e3adedbc8 to your computer and use it in GitHub Desktop.
How to use MATLAB Functionality from within Python

How to use MATLAB Functionality from within Python

Install MATLAB Engine for Python

  • Source your python environment. For me, it is anaconda with the name of virtual environment base.
source activate base
  • Change your directory to where you installed MATLAB
cd /usr/local/MATLAB/R2019a/extern/engines/python

Here, my MATLAB is installed in /usr/local/MATLAB/R2019a

  • Install matlab engine for python
python setup.py install

This will install matlab engine for python in your current python virtual environment

  • To test whether it works:
import matlab.engine

If the import is successful, you won't get any error message

An example use:

I have a matlab class in /home/ivory/VersionControl/Jmscslgroup/ROSBagReader. I want to use it from python. I will do it as follows:

eng = matlab.engine.start_matlab()
eng_return = eng.addpath('/home/ivory/VersionControl/Jmscslgroup/ROSBagReader')

References

  1. https://www.mathworks.com/help/matlab/matlab_external/matlab-arrays-as-python-variables.html
  2. https://www.mathworks.com/help/matlab/matlab_external/call-user-script-and-function-from-python.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment