Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Matlab M-script be stopped by a statement in the script?

Tags:

A very simple, and perhaps obvious, question: How can I abort execution of a Matlab M-script using a statement within the script?

This is analogous to calling return in the middle of a function to end it immediately.

like image 998
Ryan Edwards Avatar asked Feb 28 '13 22:02

Ryan Edwards


People also ask

How do you stop a script code in MATLAB?

To stop execution of a MATLAB® command, press Ctrl+C or Ctrl+Break.

How do I add a pause in MATLAB?

Typing pause(inf) puts you into an infinite loop. To return to the MATLAB prompt, type Ctrl+C. Example: pause(3) pauses for 3 seconds. Example: pause(5/1000) pauses for 5 milliseconds.

How do you end a function in MATLAB?

Use end to terminate the function.

What command or action will terminate a session MATLAB?

exit terminates the current session of MATLAB®.


2 Answers

If return is not want you need, I think you want to use break

break terminates the execution of a Matlab code. For example, statements in the loop that appear after the break statement are not executed.

In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.

like image 96
bla Avatar answered Sep 30 '22 13:09

bla


As of Matlab R2015b break can no longer be used to pre-terminate a script. A break can now only be used to a for-loop. The code will not run and an error will be thrown. This was technically always true, but it is now enforced.

The proper way is to use return

like image 29
dgmitch Avatar answered Sep 30 '22 14:09

dgmitch