How to exit a program or rather not process further code if a condition is not met ?

129 vues (au cours des 30 derniers jours)
Say for example a check for a variable 'a' ?
If a is not equal to (a~= 1) abort the program and do not execute further codes , else continue ?
Thanks,
Tonu

Réponse acceptée

Arthur
Arthur le 21 Nov 2012
Error should only be used when there is, ehm, an error. To stop the execution otherwise, use return.
  2 commentaires
Jan
Jan le 21 Nov 2012
I prefer this. Example:
if a ~= 1
return;
end
Glenn
Glenn le 17 Avr 2018
Modifié(e) : Glenn le 17 Avr 2018
The return statement returns control to the program that called the function, it doesn't terminate execution unless called from the top level.
Is there a way to gracefully stop execution within a called function?

Connectez-vous pour commenter.

Plus de réponses (2)

Tonu
Tonu le 21 Nov 2012
Hey I just used the 'error' function , and it is working fine. If any other option , please suggest, Thanks,Tonu
  1 commentaire
Aishwarya Lakshmi Srinivasan
Hi, can you please elaborate this ? In my case I am trying to abort a function that is currently running by setting the flag to false when abort button is pressed in an app designer.

Connectez-vous pour commenter.


Anmar Mohammed
Anmar Mohammed le 11 Juil 2018
Thank you very much, greetings.

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by