0

Is it possible to stop or interrupt a code in MATLAB if a condition is reached and end the simulation of the program code ? eg I have a loop that involves calculating a parameter and the moment the value becomes a complex no. I would like my code to stop executing and return the value of the counter at which the parameter value became complex.

skipper_gg
  • 1
  • 1
  • 3
  • Take a look to http://scrolls.mafgani.net/2007/03/matlab-interrupting-function-execution/ and to http://stackoverflow.com/questions/4791144/how-to-stop-a-running-script-in-matlab. – Mihai8 May 21 '13 at 13:26

1 Answers1

4

Yes, it is possible. If you want to exit your script, you can use this:

if complex(parameter)
    disp(counter);
    return;
end

If you want to exit a function and return the value of the counter to the caller, you can use this:

if complex(parameter)
    return(counter)
end

If you just want to break out of a loop, use this:

for ...
    if complex(parameter)
        break;
    end
end
print(counter)
William Payne
  • 3,055
  • 3
  • 23
  • 25