Calling matlab from python.. and have python wait for matlab to finish
조회 수: 8 (최근 30일)
이전 댓글 표시
I am attempting call matlab from a python script, and want python to wait while matlab loads, runs my script, and exits before continuing the python portion of my code.
From my research this should supposedly work:
import subprocess
from subprocess import Popen, PIPE
p = Popen("C:\\Program Files\\MATLAB\\R2008b\\bin\\matlab.exe -nosplash -r test() ", stdout = PIPE)
p.wait() # <--- blocks till done
print(p.communicate())
But when run the print statement runs well before matlab has even loaded. does anyone have any suggestions?
Thanks
Ben
댓글 수: 0
답변 (2개)
Wolfgang
2013년 11월 4일
Set pipe to non-blocking first (from http://danapeerlab.googlecode.com/svn/trunk/freecell/depends/common/python/matlabpipe.py):
import fcntl, os
flags = fcntl.fctnl(p.stdout, fcntl.F_GETFL)
fcntl.fcntl(p.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
댓글 수: 0
Juan Ramirez
2018년 5월 15일
편집: Juan Ramirez
2018년 5월 15일
Apparently this link doesn't work anymore. But you could sidestep the problem by re-structuring your code so that executing MATLAB is the final step in your python script. Then python doesn't need to wait for its completion.
Another option is to have python execute an infinite while loop that waits for a matlab output file to be written.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!