MATLAB Answers

How to stop a script if conditions are met.

4.843 views (last 30 days)
Cory on 2 Feb 2012
Commented: Walter Roberson on 30 Sep 2019
I am using the 'if' function to detect if a user inputs a number that is not 5 digits long. What I would like is a function that stops the script if a certain condition is met without displaying an error message.
P.S. I'm very new to MATLAB.


Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 2 Feb 2012
Edited: MathWorks Support Team on 9 Nov 2018
To stop running a script or function and return to the invoking function or command window, use the return function. For example,
if a > b
Any code in the current script or function that is after the return function is not run


Hao Cheng
Hao Cheng on 11 Feb 2019
"return" will terminate the current function and go back to the function that calls it. Is it possible then to stop running the entire script in GUI?
For example. A button's Callback function calls another function F. How to stop the Callback function as well if certain critieria in function F is met?

Sign in to comment.

More Answers (1)

Hamid Ramezani
Hamid Ramezani on 30 Sep 2019
you may use "error" function instead of return

  1 Comment

Walter Roberson
Walter Roberson on 30 Sep 2019
However, "without displaying an error message" is part of the requirement here.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by