MATLAB Answers

running python script in matlab

1,816 views (last 30 days)
xu on 7 Sep 2014
Commented: Edmondo Minisci on 28 Aug 2020
Hi, I have one python script and i want to execute that in matlab.I want to run that python function in matlab and saving the putput in workspace. Please can anybody help me in fixing this error. Thanks in advance!

  1 Comment

Sumeet Kumar Gupta
Sumeet Kumar Gupta on 18 Apr 2017
see this for matlab in python. but the other way is not so easy you can use subprocess module in python to call matlab and return the output.see subprocess doc. or you can do it in a more lazy way with this

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 7 Sep 2014
Xu - how complicated is your python script? Are there many inputs and several output? An interesting solution from call python function from matlab (1) exists and it involves creating a python m-file (from the MATLAB perl.m, replacing the perl text with python. I tried it, along with their example, and it worked well. Basically it involves just creating a command string and evaluating it with the MATLAB dos or unix commands (depending upon your OS).
Or, instead of creating the python.m file, you can just try with the same with a call to system. See call python function matlab (2) If your python script squares a number (like in the linked example) then you could simply write
commandStr = 'python /Users/myName/pathToScript/ 2';
[status, commandOut] = system(commandStr);
if status==0
fprintf('squared result is %d\n',str2num(commandOut));
In the above, the path to the python script is explicitly defined. If the system call is successful, then status is zero, and we print out the result. Note that since the commandOut is a string, we have to convert it to a number.
For a simple example, this works very well. For a more complicated python script, it might be a little tricker.


Show 6 older comments
Robert Snoeberger
Robert Snoeberger on 9 Sep 2016
I've noticed the following when trying to just import "pyexpat" when called from MATLAB.
>> !python -c "import pyexpat; print pyexpat.__file__"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: /usr/lib/python2.7/lib-dynload/ undefined symbol: XML_SetHashSalt
By looking at the output from "ldd" called both in MATLAB and outside of MATLAB (command line),
>> !ldd /usr/lib/python2.7/lib-dynload/ % inside MATLAB
$ ldd /usr/lib/python2.7/lib-dynload/
I noticed that some of the libraries, such as, are resolving to versions shipped with MATLAB when in MATLAB. It seems to work when I use LD_PRELOAD to preload the system version of
>> !LD_PRELOAD=/lib/x86_64-linux-gnu/ python -c "import pyexpat; print pyexpat.__file__"
kartik Sarin
kartik Sarin on 11 Feb 2017
Thanks for the input Geoff. Is there a way to integrate MATLAB with Tensorflow libraries.
Geoff Hayes
Geoff Hayes on 11 Feb 2017
Kartik - I'm not sure how to integrate MATLAB with Tensorflow libraries. Others have posted the same (see but without any response. Perhaps contact the authors of TensorFlow?

Sign in to comment.

More Answers (2)

Sarah Palfreyman
Sarah Palfreyman on 3 Oct 2014
As of MATLAB R2014b there exists bi-directional support for directly interfacing MATLAB and Python.


Show 1 older comment
Edmondo Minisci
Edmondo Minisci on 28 Aug 2020
The interface works quite well, but if I use it in a loop it gets stuck after ~3800 calls ... does any one know why, and if there is a solution for that?

Sign in to comment.

Arwel on 26 Jan 2017
I haven't tried this, but one approach I have seen is to embed a Python interpreter in C, and then to call this as a mex file. There are a few examples around (e.g. here There are other examples around if you google for them also.


Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!