Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run MATLAB code from within Python

Tags:

python

matlab

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!

like image 568
Julian Avatar asked Jul 18 '18 15:07

Julian


People also ask

Is IT possible to run MATLAB code in 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.

Can you run scripts from within the command window in MATLAB?

echodemo filename runs the script specified by filename step-by-step in the Command Window.

Can I run MATLAB code in Pycharm?

Show activity on this post. I successfully run the MATLAB code on Pycharm, let main.py is your main python file that start the python project. you need to put the invoked matlab files in the same folder './' of the main.py that starts the program and inside the main.py you need to import the engine: import matlab.


1 Answers

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

like image 137
pascscha Avatar answered Sep 22 '22 19:09

pascscha