How to continue the script while using the system()- command?

조회 수: 5 (최근 30일)
Sophie96
Sophie96 2022년 5월 6일
댓글: Riccardo Scorretti 2022년 5월 6일
I am starting a python-script out of Matlab using the system()-command.
I would to check in a while loop if the script is still running and do something else while running. If it is not running anymore, I would like to break the while-loop and continue my Matlab script. However, it seems that the while loop starts when I already closed the python-script and not while the script is still running:
checkpython = 'pgrep python';
system('unset LD_LIBRARY_PATH;python3 MyPythonScript.py')
[status1,~] = system(checkpython);
while status1==0
[status,~] = system(checkpython);
%%% Doing something else here %%%
if status == 1
disp('Python closed')
break
end
end
How could I check while the python script is running if it is still running and do something else in that while-loop?
Thanks for your advice!
Best, Sophie
  댓글 수: 1
Riccardo Scorretti
Riccardo Scorretti 2022년 5월 6일
In order to continue the execution of MATLAB while running the pyhton script, you could add an ampersand (&) to the end of the command.
As for checking if your python script terminated the execution, I cannot figure out any elegant solution. The "quick and dirty" method could be to make your script open a dummy file at the beginning, and delete it when he finishes; so you can check if the script is still running by checking the existance of that particular file.

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

답변 (1개)

Sophie96
Sophie96 2022년 5월 6일
Perfect, the ampersand was the solution I was looking for. Thanks!

카테고리

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