running python script in matlab

조회 수: 14 (최근 30일)
xu
xu 2014년 9월 7일
답변: Andreas Urbán 2022년 3월 28일
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일
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
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일
As of MATLAB R2014b there exists bi-directional support for directly interfacing MATLAB and Python.
  댓글 수: 4
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일
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

댓글을 달려면 로그인하십시오.


Andreas Urbán
Andreas Urbán 2022년 3월 28일

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by