skipper_gg
skipper_gg

Reputation: 1

Is it possible to stop or interrupt a code in MATLAB if a condition is reached and end the simulation of the program code ?

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.

Upvotes: 0

Views: 5109

Answers (1)

William Payne
William Payne

Reputation: 3325

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)

Upvotes: 4

Related Questions