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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!