Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Matlab using Python gives 'No module named matlab.engine' error

Tags:

python

matlab

I am trying to run Matlab code using Python. I tried to follow the instructions given on this Mathworks page.

When trying to import Matlab though Python, it was installed using pip install matlab.

However, importing matlab.engine gives the error No module named 'matlab.engine'; 'matlab' is not a package. It cannot be installed using pip install engine either.

How can I get the code running? The Python code I'm running is as below:

import matlab.engine
eng = matlab.engine.start_matlab()
eng.triarea(nargout=0)

Python version - 3.5

Matlab version - 8.5.0.197613 (R2015a)

like image 243
Audrey Avatar asked Sep 10 '17 14:09

Audrey


People also ask

Can I run MATLAB in Python?

Start MATLAB Engine for PythonImport the matlab. engine package into your Python session. Start a new MATLAB® process by calling start_matlab . The start_matlab function returns a Python object eng which enables you to pass data and call functions executed by MATLAB.

How do you call a MATLAB function from Python?

There are two approaches for calling MATLAB code from Python. The first is to use the MATLAB Engine API for Python, which requires a MATLAB install. The second is to use MATLAB Compiler SDK to compile a Python package that does not require users to have a MATLAB install.


1 Answers

You need to install the Matlab Engine for Python, and it cannot be installed using pip. Try the instructions listed here. I have listed the instructions briefly below:

  1. Make sure you have Python in your PATH.
  2. Find the Matlab root folder. You can use the matlabroot command within Matlab to find it.
  3. Go to the Matlab root folder in the command line.
  4. cd "matlabroot\extern\engines\python" (In Windows)
  5. python setup.py install
like image 97
Fleur Avatar answered Sep 18 '22 12:09

Fleur