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

138 ビュー (過去 30 日間)
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

採用された回答

Arthur
Arthur 2012 年 11 月 21 日
Error should only be used when there is, ehm, an error. To stop the execution otherwise, use return.
  2 件のコメント
Jan
Jan 2012 年 11 月 21 日
I prefer this. Example:
if a ~= 1
return;
end
Glenn
Glenn 2018 年 4 月 17 日
編集済み: Glenn 2018 年 4 月 17 日
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?

サインインしてコメントする。

その他の回答 (2 件)

Tonu
Tonu 2012 年 11 月 21 日
Hey I just used the 'error' function , and it is working fine. If any other option , please suggest, Thanks,Tonu
  1 件のコメント
Aishwarya Lakshmi Srinivasan
Aishwarya Lakshmi Srinivasan 2021 年 3 月 8 日
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.

サインインしてコメントする。


Anmar Mohammed
Anmar Mohammed 2018 年 7 月 11 日
Thank you very much, greetings.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by