running python script in matlab

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!
Regards

1 件のコメント

Sumeet Kumar Gupta
Sumeet Kumar Gupta 2017 年 4 月 18 日
編集済み: Sumeet Kumar Gupta 2017 年 4 月 18 日
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

サインインしてコメントする。

 採用された回答

Geoff Hayes
Geoff Hayes 2014 年 9 月 7 日

5 投票

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/sqr.py 2';
[status, commandOut] = system(commandStr);
if status==0
fprintf('squared result is %d\n',str2num(commandOut));
end
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.

9 件のコメント

xu
xu 2014 年 9 月 10 日
Hello geoff, Thanks for your response.My python script has many outputs and I've adapted the perl.m to python.m successfully, so I can process these data in MATLAB. Thanks for your help!
Geoff Hayes
Geoff Hayes 2014 年 9 月 10 日
Glad it worked out, Xu!
Mario Martos
Mario Martos 2016 年 5 月 11 日
Hello, I have done this, but with Ubuntu and Matlab 2015a I have this problem or error, and I don't know what happen... Any ideas?
cmdout =
type 'exceptions.ImportError'> occurred: No module named expat; use SimpleXMLTreeBuilder instead in Bach789Sco.xml
Thanks in advanced.
Karel Lebeda
Karel Lebeda 2016 年 8 月 5 日
I have a similar problem to Mario, I get the following:
ImportError: PyCapsule_Import could not import module "pyexpat"
but when I run the same script directly from bash, it works without any problems... I checked my PYTHONPATH in both and they seem the same.
Geoff Hayes
Geoff Hayes 2016 年 8 月 5 日
Karel - you may want to show how you are calling your script (from MATLAB) and describe what version and OS you are using.
Rik Baehnemann
Rik Baehnemann 2016 年 9 月 9 日
I have the same issue. Running Ubuntu 16.04 and Matlab2016a. I have ROS kinetic on the system if that is relevant.
ImportError: PyCapsule_Import could not import module "pyexpat"
Robert Snoeberger
Robert Snoeberger 2016 年 9 月 9 日
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/pyexpat.so: 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/pyexpat.so % inside MATLAB
$ ldd /usr/lib/python2.7/lib-dynload/pyexpat.so
I noticed that some of the libraries, such as libexpat.so.1, 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 libexpat.so.1.
>> !LD_PRELOAD=/lib/x86_64-linux-gnu/libexpat.so.1 python -c "import pyexpat; print pyexpat.__file__"
/usr/lib/python2.7/lib-dynload/pyexpat.so
>>
kartik Sarin
kartik Sarin 2017 年 2 月 11 日
Hallo,
Thanks for the input Geoff. Is there a way to integrate MATLAB with Tensorflow libraries.
Geoff Hayes
Geoff Hayes 2017 年 2 月 11 日
Kartik - I'm not sure how to integrate MATLAB with Tensorflow libraries. Others have posted the same (see https://www.mathworks.com/matlabcentral/answers/314283-how-can-i-use-tensorflow-library-from-matlab-environment) but without any response. Perhaps contact the authors of TensorFlow?

サインインしてコメントする。

その他の回答 (3 件)

Sarah Palfreyman
Sarah Palfreyman 2014 年 10 月 3 日

4 投票

As of MATLAB R2014b there exists bi-directional support for directly interfacing MATLAB and Python.

4 件のコメント

Geoff Hayes
Geoff Hayes 2014 年 10 月 4 日
Sarah - could you describe what exactly this means, or provide a reference/link that provides more details (with examples)?
Ken Atwell
Ken Atwell 2014 年 10 月 6 日
Geoff,
Two links for more information and examples:
Geoff Hayes
Geoff Hayes 2014 年 10 月 6 日
Thanks, Ken!
Edmondo Minisci
Edmondo Minisci 2020 年 8 月 28 日
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?

サインインしてコメントする。

Arwel
Arwel 2017 年 1 月 26 日

0 投票

HI,
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 http://stackoverflow.com/questions/1700628/embed-python-in-matlab-mex-file-on-os-x).... There are other examples around if you google for them also.
Arwel

1 件のコメント

Ronan Fleming
Ronan Fleming 2021 年 4 月 12 日
https://nl.mathworks.com/help/releases/R2021a/matlab/matlab_external/out-of-process-execution-of-python-functionality.html

サインインしてコメントする。

カテゴリ

質問済み:

xu
2014 年 9 月 7 日

回答済み:

2022 年 3 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by