Script Within a Script - Return

I have a a situation where Script 1 calls Script 2 to perform a logic check. If this logic check fails, I want the entire program (Script 1 and Script 2) to stop. Obviously, the Return command will only stop Script 2, and Script 1 will continue to run to it's finish. Is there an elegant way to do this? A simplified code example is below.
%%SCRIPT_1
disp('Running Script 1');
Script_2 %%Call Script 2
disp('Still Running Script 1');
%%SCRIPT_2
happy = input('Do you want these scripts to continue running? y or n?');
if happy == 'y'
disp('Cool');
else
return
end
So, in this instance, even if you tell Script 2 'n', Script 2 will stop and then Script 1 will continue. I understand the code and why this occurs. What I would like is if you tell Script 2 'n', that Script 1 will stop as well.

4 comentarios

Rik
Rik el 12 de Sept. de 2017
Also as a general side-note: it is usually better practice to use functions instead of scripts, to avoid unintentional changes of variables.
Cody Miller
Cody Miller el 13 de Sept. de 2017
Rik, We have several instruments that do the same thing, but output data in slightly different ways. These are mechanical testing frames. I am attempting to write a series of scripts to modularize the data analysis, as sometimes we simply want to clean up the data, but other times we might calculate strain rate, yield stress, and various other properties. The thought is you can write a simple script that only calls the necessary scripts for analysis, and it minimizes your coding work.
Based on this, would you still suggest using functions instead?
Stephen23
Stephen23 el 13 de Sept. de 2017
Editada: Stephen23 el 13 de Sept. de 2017
"...would you still suggest using functions instead?"
Yes, always.
Scripts are for playing around with a new idea, but are not suitable for any serious, repeatable, robust code. The shared workspace of scripts is a nightmare. Avoid them as much as you can.
Rik
Rik el 14 de Sept. de 2017
I still would as well. You can have a main m-file (which can still be a script), which just calls all necessary functions. With input parsing (functions like validateattributes, narginchk and the like) you can easily separate out cases. A shared workspace can cause bugs that are very hard to find.
Also, if my answer helped you, please mark it as accepted answer.

Iniciar sesión para comentar.

Respuestas (1)

Rik
Rik el 12 de Sept. de 2017
use a second output for the inner function:
%%SCRIPT_1
disp('Running Script 1');
Script_2 %%Call Script 2
if ReturnFlag,return,end
disp('Still Running Script 1');
%%SCRIPT_2
ReturnFlag=false;
happy = input('Do you want these scripts to continue running? y or n?');
if happy == 'y'
disp('Cool');
else
ReturnFlag=true;
return
end

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Sept. de 2017

Comentada:

Rik
el 14 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by