How to stop a script if conditions are met.

1.532 visualizaciones (últimos 30 días)
Cory el 2 de Feb. de 2012
Comentada: Walter Roberson el 5 de Nov. de 2022
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 comentario
Jingyang Xie
Jingyang Xie el 6 de En. de 2021
Hi Cory, have you solved this problem? I think I encountered the same problem...

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 2 de Feb. de 2012
Editada: MathWorks Support Team el 9 de Nov. de 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
  9 comentarios
Muhammad Usman Saleem
Muhammad Usman Saleem el 5 de Nov. de 2022
Respected Sir @Walter Roberson, I not get the point your shared. Will you please amend this suggestion in my code shared above please?
Walter Roberson
Walter Roberson el 5 de Nov. de 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};

Iniciar sesión para comentar.

Más respuestas (1)

Hamid Ramezani
Hamid Ramezani el 30 de Sept. de 2019
you may use "error" function instead of return
  3 comentarios
Adam K
Adam K el 10 de Dic. de 2020
Editada: Adam K el 10 de Dic. de 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)
Rik el 10 de Dic. de 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.

Iniciar sesión para comentar.


Más información sobre Logical en Help Center y File Exchange.


Community Treasure Hunt

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

Start Hunting!

Translated by