Install Matlab Engine API in Python

1 minute read

Related: Call Python from Matlab

Alternative: Call Matlab .m function code using free open-source GNU Octave from Python with oct2py.

Matlab Engine API works from Python 3.6, 3.5, 3.4 and 2.7. That is, you can call Matlab functions from Python code!


cd $(dirname $(realpath $(which matlab)))/../extern/engines/python

python build --build-base=$(mktemp -d) install

Permissions Error

If you get a complaint about root permissions, you’re probably installing in system Python. I normally recommend using Anaconda Python instead, but if you must use system Python:

python build --build-base=$(mktemp -d) install --user


cd %PROGRAMFILES%/MATLAB/R2017b/extern/engines/python

python build --build-base=%TEMP% install


Python → Matlab

import matlab.engine
eng = matlab.engine.start_matlab('-nojvm')


The -nojvm Matlab Engine option is important for not having the Matlab Engine hang forever upon startup. It should take less than 1 second for Matlab Engine to start when called from Python.

matlab.double → Numpy

enclose the matlab.double variable in numpy.asarray()

Python float → Matlab Engine

Python floats pass into Matlab Engine by including a period . after the number.

  • asin(1) fails
  • asin(1.) works

numpy.ndarray → Matlab Engine

Unlike the py module in Matlab, which can only pass 1 x N arrays, Matlab Engine from Python can pass N-D arrays.  


Leave a Comment