How to stop a script if conditions are met.

2.475 views (last 30 days)
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.
  1 Comment
Jingyang Xie
Jingyang Xie on 6 Jan 2021
Hi Cory, have you solved this problem? I think I encountered the same problem...

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
Walter Roberson
Walter Roberson on 5 Nov 2022
notdone = true(9,1);
while any(notdone)
if rcp(i,m)<0.1 & trcp(i,m) > 0.8
notdone(1) = false;
if recp(i,m)<0.1 & trecp(i,m) > 0.8
ECN{i,m}= Ecn{i,m};

Sign in to comment.

More Answers (1)

Hamid Ramezani
Hamid Ramezani on 30 Sep 2019
you may use "error" function instead of return
Rik on 10 Dec 2020
I wonder if it is possible to detect if the code is running in a try block. In such cases the message might be more confusing than simply throwing the error.

Sign in to comment.


Find more on Programming Utilities in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!