How to cancel execution of second matlab script, when started 2 scripts in a row
조회 수: 2 (최근 30일)
이전 댓글 표시
Here's a problem I had for several years now, and never found an easy solution for it.
I have two matlab scripts, say script1.m and script2.m. These are both scripts which take several hours of calculation. I ran script1.m and waited for a couple of hours. Next (while script1.m was still executing) I accidently pressed the "Run" button for script2.m in the same matlab window. script2.m starts with "close all; clear all;" and hence all my results will be erased when script2.m starts, which is like 1 millisecond after the completion of script1.m. Is there a way to prevent matlab from executing script2.m (without changing my programming style)?
I could ofcourse modify script2.m and put the command "return" on its first line and save it, but I would love to know if there's a more elegant way of doing this (without changing my programming style)?
댓글 수: 0
채택된 답변
Daniel Shub
2012년 4월 27일
What about adding to the end of script 1:
save
!matlab -r load matlab.mat &
exit
It will save your workspace, launch a new MATLAB which will automatically load your workspace, and then quit the current MATLAB.
It is ugly, but you will not have to adjust your programming style.
댓글 수: 2
Daniel Shub
2012년 5월 2일
But adding return to the beginning of script2 means it will never run. If you are constantly editing a script to make it run/not run, that seems like a disaster waiting to happen. The return approach also means you need to add that control to all possible scripts that begin with close all; clear all. You only have to add my approach to scripts which take a long time to compute.
추가 답변 (1개)
Daniel Shub
2012년 4월 27일
Yes, there is a more elegant way. Convert script 1 and 2 into functions. Use structures to store and pass your variables.
I am going to let Jan handle the inelegance of "clear all" ...
댓글 수: 4
Daniel Shub
2012년 4월 27일
You should modify your question to give us all the constraints. You asked in your question if it could be done and done in an elegant way. I told you yes. No you come back and say without changing your programing style. If that is a requirement, then you should add it to your question.
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!