Skip to content

MATLAB Engine API Setup for PyCharm Integration

This guide explains how to run a MATLAB .m script from a Python .py script using the MATLAB Engine API in PyCharm.


Outlook

I took this video as a starting point, tried both ways, but found the Matlab engine API the feasible method. (however you’re more than welcome to solve the other one)
Let’s assume ‘main.py’ is the main Python file which runs your Python project & ‘main.m’ is the main Matlab file which you’d like to run in PyCharm. For this, you need to
1) put the ‘main.m’ file in the same folder as the ‘main.py’,
2) connect Matlab with PyCharm with a Matlab Engine API for Python,
3) import the Matlab engine into the ‘main.py’ and call the Matlab code.
Keep in mind, that for this method, you need Matlab to be installed.


Step 0: Version Compatibility

  • First and foremost, check the version of Python which you’re working with. (If you're working in .venv, check that as well and take that into account too). Also check that you’re working with a 64 bit version of Python to match the architecture of Matlab.
  • Check your Matlab release version too.
  • Crucial step!: Check the compatibility of your Python and Matlab here.

Step 1: Install MATLAB Engine API

  1. The matlab engine API is within the installed MATLAB package. To avoid any permission errors and other issues run cmd as an Administrator (OR if you’re running your ‘main.py’ from a virtual environment -in my case anaconda3- open anaconda prompt as an Administrator).
  2. While in the virtual environment, direct to the API folder, which is usually in the following path:
C:\Program Files\MATLAB\[your_version]\extern\engines\python

If you direct to this path, you should see the ‘setup.py’ file. 3. In the command line, install this with the following command:

python setup.py install

For alternative installation methods, refer to the official guide.


Step 2: Place .m and .py Files are in the same folder

Put your main.m file in the same folder where you run your main.py file from.


Step 3: Import the main.engine

In your main.py:

import matlab.engine

If you get the `No module named 'matlab' error, then your API installation wasn’t successful:(( Check if you installed the engine to the right path.


Step 4: Test the Connection

Check if you were successful and matlab is connected to PyCharm with the following code: You can also see how you can start the engine here.

import matlab.engine
eng = matlab.engine.start_matlab()
eng.eval("disp('Connected to MATLAB')", nargout=0)
eng.quit()

Step 5: Pass Parameters from Python to MATLAB

So far, the setup can run a MATLAB code, but can not overwrite the variables dynamically. To overwrite a variable in Python and ensure MATLAB uses it when you call the MATLAB function via the API, you can pass it as an argument to your MATLAB function.

Theoretically, the other option would be to assign it directly in the MATLAB workspace with eng.workspace[‘data_path’] = …

HOWEVER for me, it resulted in an error saying unrecognised function or variable, which confirms that (in this case) data_path is not visible in the function scope of the MATLAB script. This presumably is because variables set in the MATLAB base workspace (via Python eng.workspace['...'] = ...) are not automatically visible inside the functions. Therefore, we’re left with passing the variables as arguments. For this, you have to modify the code to a function that accepts the data_path and other variables as inputs. (make sure you don’t clear the assigned variables).


Step 6: Run the Python Code

Run the MATLAB code via the API, you can call the MATLAB script as if it were a Python function with:

 eng.[matlab_script_name ](params, nargout = 0)

🎉 Done!

Congratulations — your Python project now integrates MATLAB! 🤯🥳