This repo gives examples of calling Matlab functions from Python using Matlab Engine for Python.
Install necessary packages for these examples:
conda install --file requirements.txt
# OR
python -m pip install -r requirements.txt
Python can directly exchange data with Matlab and call Matlab functions via Matlab Engine for Python. Setup Matlab Engine for Python:
python -m pip install matlabengine
Or install directly from Matlab directory like:
python -m pip install $(matlab -batch "disp(matlabroot)" | tail -n1)/extern/engines/python/
Article explains more details of Matlab Engine for Python.
Matlab Engine accepts Numpy.ndarray into Matlab functions. Getting Numpy.ndarray from Matlab Engine returned arrays requires like:
import numpy
import matlab.engine
eng = matlab.engine.start_matlab("-nojvm")
x = numpy.random.random((3,2))
y_m = eng.my_matlab_fun(x)
y = numpy.array(y_m.tomemoryview()).reshape(y_m.size, order="F")
For Matlab functions that return more than one output, use "nargout=" like:
import matlab.engine
eng = matlab.engine.start_matlab("-nojvm")
x, y = eng.myfun(x, nargout=2)
To avoid using Matlab Engine, which requires compatible versions of Python and Matlab, one can interchange data using a file and calling the other language interpreter.
Example: image_hdf5.py calls Matlab function image_hdf5.m.
Matlab Engine example: image_matlab_engine.py.
Matlab can use Python code via pyenv.
Example: image_proc.m.
check_numpy.m shows how to check issues with DLLs on Windows with Numpy and Matlab.
Python importlib can also give hints about problems importing:
py.importlib.import_module('numpy')
Other things to try especially on Windows include:
setenv("KMP_DUPLICATE_LIB_OK", "TRUE")
this avoids Matlab crash below--is this error from Matlab libiomp5md.dll loaded and Numpy also loads that DLL?
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.