27

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!

LightCC
  • 9,804
  • 5
  • 52
  • 92
ATP
  • 603
  • 1
  • 9
  • 14

1 Answers1

32

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 :)

pascscha
  • 1,623
  • 10
  • 16
  • 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 – ATP Jul 18 '18 at 16:50
  • That's weird, I have Python 3.6 as well. But my Matlab version is 2017b maybe that's the issue. – pascscha Jul 18 '18 at 16:52
  • MathWorks says Python 3.6 is fine and that the Matlab version should be 2014b or later, and mine is... Ugh – ATP Jul 18 '18 at 16:55
  • @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 – pascscha Jul 18 '18 at 17:04
  • I wrote a workaround to solve the python 3.6 problem. Here’s the gist: https://gist.github.com/dangom/303879ca649f7a3fe113912d7ffa62d4 – Daniel Jul 18 '18 at 20:19
  • @pasch does that mean I need to include all of my Matlab code and save it in the variable called script? – ATP Jul 19 '18 at 14:13
  • @dangom do you know where I can access the file engines/python/build/lib/matlab/engine/__init__.py ? I'm using a mac and I have no idea where to locate it. Thanks! – ATP Jul 19 '18 at 14:21
  • @Julian Yes, all the matlab code in the variable script will be saved in a matlab file and then executed. I also really encourage you to use the octave solution, since i think it will cause you way less worry than matlab while the functionality stays the same. – pascscha Jul 19 '18 at 14:25
  • @pasch Maybe I'm misunderstanding your code but I already have a .m file with a bunch of lines of code, meaning I don't want to create a variable (script) that will be saved as a matlab file because I already have a matlab file I want to use. In other words, is there a way to just call that matlab file without having to create a 'script' variable? – ATP Jul 19 '18 at 14:32
  • @Julian no, then you just have to put this file in the same folder as your python script and run `oc.filename` – pascscha Jul 19 '18 at 14:53
  • Thanks! So my matlab code isn't a function, it's just a regular code. I'm running the python code and there are no errors but nothing happens. My matlab code is supposed to plot a simple graph but nothing seems to be happening. – ATP Jul 19 '18 at 15:09
  • also if I try making it a function and I call it appropriately on my Python code (the same way you did in your code) I get this error: error: invalid call to script /Users/gnss_user/Documents/PMTest.m, where PMTest.m is the name of my matlab file – ATP Jul 19 '18 at 15:13
  • @Julian You could also just use `matplotlib` if you just want to plot something... – pascscha Jul 19 '18 at 15:43
  • so if I already have a myScript.m, then I can just do oc.myScript(7)? – June Wang Aug 25 '19 at 17:18
  • June's question is exactly mine, @pascscha any ideas ? – FabioSpaghetti Nov 22 '19 at 13:16
  • 3
    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(...)`. – jds Mar 01 '20 at 16:36
  • @gwg what do you put inside the file please ? Do you define some variables? – Marine Galantin Apr 29 '20 at 10:17
  • In `foo.m` would be a MATLAB function `foo`. I would suggest trying that simple example; make a MATLAB file, `foo.m` with a function `foo` that just prints "foo". Then try calling it with Python using the above, using `eng.foo()`. – jds Apr 29 '20 at 11:25
  • OSError: octave-cli not found, please see README – The Singularity Dec 27 '20 at 16:38
  • Is there a way to run the matlab script without hardcoding the name of the .m file in the Python code? In my case, the Python code will be reading the name of the matlab script from a text file. So I can't call eng.MyScript() from Python. – Mikutus Jan 07 '22 at 20:12