28

I am trying to run a MATLAB code using Python (I'm using python 3.6).

I don't need to pass any arguments or get any outputs. I just need a line of code on Python that will simply run the MATLAB code.

I saw some answers online that say to use matlabroot and to use that in the command prompt to install some sort of engine but it said I couldn't install it because my Python version was not old enough (which makes no sense).

Is there an easier version or just another way to do this?

Thanks!

2
  • 1
    Do you have Matlab installed on your computer? Commented Jul 18, 2018 at 16:02
  • Yes, I have the 2016b version Commented Jul 18, 2018 at 16:08

1 Answer 1

34

Using Oct2Py

Your first option is using Oct2Py which runs with Octave, a free and opensource Program that can run Matlab files and functions. Just install it with the following Terminal command:

pip3 install oct2py

Then you can run MatLab Code from your Python script like that:

from oct2py import Oct2Py
oc = Oct2Py()


script = "function y = myScript(x)\n" \
         "    y = x-5" \
         "end"

with open("myScript.m","w+") as f:
    f.write(script)

oc.myScript(7)

Using MatLab

If you want to use the original MatLab engine you would have to follow the following steps:

1. Installing the MatLab library

Following the instructions of this page you first have to find your MatLab root folder by opening MatLab and running the command matlabroot. This should give you the root folder for Matlab.

Then you open your terminal (if you are using Windows you can do that by pressing Windows + R, then type cmd and press Enter.) In the terminal you run following code:

cd matlabroot\extern\engines\python

Make sure to replace matlabroot with the Path you just found. Then you run

python3 setup.py install

To install the MatLab Python library.

2. Using the MatLab Library

Following the instructions of this page You can then

import matlab.engine
    
eng = matlab.engine.start_matlab()
tf = eng.isprime(37)
print(tf)

If you want to run entire scripts, you can save your scripts as a MatLab *.m file in your current folder and run them like this:

import matlab.engine
eng = matlab.engine.start_matlab()
eng.myMatlabFile(nargout=0) # Expects a file named myMatlabFile.m in the same directory

You could also create the MatLab File from Python:

import matlab.engine

script = "b = 5;\n" \
         "h = 3;\n" \
         "a = 0.5*(b.* h)"

with open("myScript.m","w+") as f:
    f.write(script)

eng = matlab.engine.start_matlab()
eng.myScript(nargout=0)

I hope this helps :)

Sign up to request clarification or add additional context in comments.

20 Comments

Thanks for your reply! I'm running it on a mac and when I try to run python3 setup.py install I get this: OSError: MATLAB Engine for Python supports Python version 2.7, 3.3, 3.4 and 3.5, but your version of Python is 3.6
That's weird, I have Python 3.6 as well. But my Matlab version is 2017b maybe that's the issue.
MathWorks says Python 3.6 is fine and that the Matlab version should be 2014b or later, and mine is... Ugh
@Julian You could also just use Oct2Py, it's free and open source and it has a python bridge as well :) I just updated my answer so it includes instructions for Oct2Py
If anyone else is confused, myMatlabFile.m is the actual filename. It's not a function that you pass in a filename to. So if your file is foo.m, you call eng.foo(...).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.