How to stop a script if conditions are met.

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.

2 Kommentare

Jingyang Xie
Jingyang Xie am 6 Jan. 2021
Hi Cory, have you solved this problem? I think I encountered the same problem...
Paul
Paul am 23 Sep. 2025
A simple way to stop execution in MATLAB without throwing an error is to use the return or break statements, depending on whether the code is inside a function or loop. This prevents unwanted error messages and keeps the script clean.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 2 Feb. 2012
Bearbeitet: MathWorks Support Team am 9 Nov. 2018

18 Stimmen

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
return
end
Any code in the current script or function that is after the return function is not run

9 Kommentare

Hao Cheng
Hao Cheng am 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?
If i have multiple conditions and i want to run get each condition according to my requirement, then can I use if and else if condition please?
if rcp(i,m)<0.1 & trcp(i,m) > 0.8;
ECP{i,m}=Ecp{i,m};
elseif recp(i,m)<0.1 & trecp(i,m) > 0.8;
ECN{i,m}= Ecn{i,m};
elseif recnn(i,m)<0.1 & trecnn(i,m) > 0.8;
ECNN{i,m}= Ecnn{i,m};
elseif recl(i,m)<0.1 & trecl(i,m) > 0.8;
ECL{i,m}= Ecl{i,m};
elseif recs(i,m)<0.1 & trecs(i,m) > 0.8;
ECS{i,m}= Ecs{i,m};
elseif recpp(i,m)<0.1 & trecpp(i,m) > 0.8;
ECPP{i,m}= Ecpp{i,m};
elseif recm(i,m)<0.1 & trecm(i,m) > 0.8;
ECM{i,m}= Ecm{i,m};
elseif recy(i,m)<0.1 & trecy(i,m) > 0.8;
ECY{i,m}= Ecy{i,m};
elseif recmm(i,m)<0.1 & trecmm(i,m) > 0.8;
ECMM{i,m}= Ecmm{i,m};
break,
end
There are 9 variables inside my for loop and I want to execute my variables according to certian value which I used in if conditions. Whether for loop will continue to run till all my conditions become fulfill or just break when my first condition rcp(i,m)<0.1 & trcp(i,m) > 0.8 will be met sir?
I want to stop processing on my variable when it reach my condition, I want to continue processing on my other variables untill their condition meet? Is the above logic i am using is fine or not please?
In the code you posted, the break will only happen if all of the other conditions before that are false, until finally recmm(i,m)<0.1 & trecmm(i,m) > 0.8
Dear @Walter Roberson I want to run for loop untill my all 9 conditions becomes true as you highlights break will work until all above conditions before the last ones become false. How can I run for loop untill my all 9 conditions achieved please?
create a vector notdone = true(9,1)
while(any(notdone))
then as you do tests and find you have satisfied a condition, set notdone(ConditionNumber) = false for appropriate ConditionNumber
Respected Sir @Walter Roberson, I not get the point your shared. Will you please amend this suggestion in my code shared above please?
notdone = true(9,1);
while any(notdone)
if rcp(i,m)<0.1 & trcp(i,m) > 0.8
ECP{i,m}=Ecp{i,m};
notdone(1) = false;
end
if recp(i,m)<0.1 & trecp(i,m) > 0.8
ECN{i,m}= Ecn{i,m};
notdone(2)=false;
end
%etc
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Hamid Ramezani
Hamid Ramezani am 30 Sep. 2019

3 Stimmen

you may use "error" function instead of return

3 Kommentare

Walter Roberson
Walter Roberson am 30 Sep. 2019
However, "without displaying an error message" is part of the requirement here.
Adam K
Adam K am 10 Dez. 2020
Bearbeitet: Adam K am 10 Dez. 2020
I think a 'msgbox' to really alert the user what is the issue, and then the 'error' funcition to stop the code would be a workaround. This way the user is aware that the issue is not really an 'error' when they see the red error font.
msg = "You've got a number that is not 5 digits long";
f = msgbox(msg)
error(msg)
Rik
Rik am 10 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Korosh Agha Mohammad Ghasemi
Verschoben: Voss am 25 Jun. 2024

0 Stimmen

To stop a script in MATLAB without displaying an error message when a user inputs a number that is not 5 digits long, you can use the return function. The return function will exit the function or script without an error message. Here is an example:
% Request user input
userInput = input('Please enter a 5-digit number: ', 's');
% Check if the input is 5 digits long
if length(userInput) ~= 5
fprintf('Input must be a 5-digit number. Exiting script.\n');
return;
end
% Continue with the rest of your script
disp('Input is valid. Continuing script...');
% ... rest of your script
Explanation:
  1. Request User Input: Use input to get the user input as a string.
  2. Check the Length: Use length to check if the input length is not equal to 5.
  3. Return Function: Use return to exit the script if the condition is met, and display a message using fprintf.
This will ensure that the script stops if the user input is not a 5-digit number, and it will do so without throwing an error.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 2 Feb. 2012

Kommentiert:

am 23 Sep. 2025

Community Treasure Hunt

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

Start Hunting!

Translated by