This is a short story about how I had to connect a small Matlab script with Python!. Some scripts in Matlab calculated 3D segmentation over images using Matlab's functions. On the other side, some code in Tensorflow which pre-process CT (computed tomography) scan data, then they should be segmented to feed the neural network in Tensorflow.
Then, to forward in the project and do not rewrite all the segmentation code (already done and working in Matlab), the forth-and-back Python with Matlab was necessary.
Let me start by noticing the Python version is 3.8 and my Matlab's version was R2019a. According to the documentation, that version only supports Python 3.7. Subsequently, the first step is installing MATLAB R2020b (version 9.9) which supports CPython 3.8 (in addition to existing support for 2.7, 3.6, and 3.7).
Once everything is correctly installed, let me show a simple example of this process. Of course, for further information, you can check the official documentation.
Install the MATLAB Engine API for Python
The engine is the mechanism used to connect Matlab with other programming languages like C, C++, Java, .NET, web services and Python. It is mandatory the installation of the engine for the
First, we need to locate where Matlab is installed. Once there to proceed to the installation. To discover the location of Matlab, we open it and type the following
matlabroot ans = 'C:\Program Files\MATLAB\R2020b'
Inside that folder, execute the setup.py script, and it would copy and execute others scripts:
cd C:\Program Files\MATLAB\R2020b\extern\engines\python python setup.py install running install running build running build_py creating build creating build\lib creating build\lib\matlab copying dist\matlab\mlarray.py -> build\lib\matlab copying dist\matlab\mlexceptions.py -> build\lib\matlab copying dist\matlab\__init__.py -> build\lib\matlab # ommited output on purpose ... byte-compiling C:\Python\Python368\Lib\site-packages\matlab\__init__.py to __init__.cpython-36.pyc running install_egg_info Writing C:\Python\Python368\Lib\site-packages\matlabengineforpython-R2020b-py3.6.egg-info
Now, the Matlab engine is already installed. Next, create the Matlab code to be invoked and the Python code which invoked the Matlab's script.
Writing the Matlab code
Using the engine in Python, it is possible to call any function provided by Matlab. Actually, it is possible to call native functions from Matlab and the included in incorporated packages.
However, in this case, I just focus on a simple user-defined function:
function [r] = test_fn(M) r = prod(M(:));
test_fn receives a matrix
M and returns the multipliccation of all its values. Notice that it is possible to create any user-defined function from the Matlab side.
Great, from this side everything is done. Next, the Python code is the last piece.
Finally, the Python code
One of the most important steps is about passing data from Python as an input argument to Matlab. This is an important point to check where I recommend to further read the official documentation of MathWorks.
In Python, it is important to import the matlab.engine that provide functions to call MATLAB, and it contains the array classes provide functions to create MATLAB arrays as Python objects. For instance, to create an array of doubles you should use
matlab.double datatype or for an array of float, then use
Moreover, functions contained in the engine are invoked as functions included in Matlab's object. Let's see the code:
import matlab.engine eng = matlab.engine.start_matlab() M = matlab.single([[1, 2, 3], [4, 5, 6]]) print(eng.test_fn(M))
The script creates a
2x3 float matrix and passes it to the
test_fn function created in Matlab, which returns a single value (
float in this case). Finally, we could execute the python script saved in
test.py as follows:
python test.py 720.0
The result is
1 x 2 x 3 x 5 x 6 (which is actually
6!), and that's it! This is a single template code to call Matlab script in Python.
From a geek to geeks