Azzera filtri
Azzera filtri

How to cancel execution of second matlab script, when started 2 scripts in a row

2 visualizzazioni (ultimi 30 giorni)
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)?

Risposta accettata

Daniel Shub
Daniel Shub il 27 Apr 2012
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 Commenti
Geert
Geert il 2 Mag 2012
I regard this answer as equivalent to adding "return" at the beginning of script2.m (where the latter is something that can still be done when script1 is executing). It is an ugly solution but it works :).
It seems to be the best solution without changing too much of my programming style.
I was hoping there was a option in the matlab interface where you could see which scripts/commands are going to execute consecutively, and where you could cancel some of these scipts/commands :)...
thanks!
Daniel Shub
Daniel Shub il 2 Mag 2012
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.

Accedi per commentare.

Più risposte (1)

Daniel Shub
Daniel Shub il 27 Apr 2012
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 Commenti
Daniel Shub
Daniel Shub il 27 Apr 2012
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.

Accedi per commentare.

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by