How to stop a script if conditions are met.

1,921 次查看(过去 30 天)
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 个评论
Jingyang Xie
Jingyang Xie 2021-1-6
Hi Cory, have you solved this problem? I think I encountered the same problem...

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2012-2-2
编辑:MathWorks Support Team 2018-11-9
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 个评论
Walter Roberson
Walter Roberson 2022-11-5
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

请先登录,再进行评论。

更多回答(1 个)

Hamid Ramezani
Hamid Ramezani 2019-9-30
you may use "error" function instead of return
  3 个评论
Rik
Rik 2020-12-10
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.

请先登录,再进行评论。

类别

Find more on Startup and Shutdown 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!

Translated by