Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you pause MATLAB? [duplicate]

In MATLAB, I'm running some code which takes a while to run. I'd like to pause the code to check on some variable values. Is there a way I can do this without having to re-run the code from the beginning? I don't want to terminate the program; just pause it.

like image 588
Shen Avatar asked Sep 17 '13 20:09

Shen


People also ask

Can I pause MATLAB execution while it is already running?

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 I pause MATLAB running code?

To stop execution of a MATLAB® command, press Ctrl+C or Ctrl+Break. On Apple Macintosh platforms, you also can use Command+. (the Command key and the period key).

Is there a wait function in MATLAB?

The wait function can be useful when you want to guarantee that data is acquired before another task is performed. wait(obj,waittime) blocks the MATLAB command line until the video input object or array of objects obj stops running or until waittime seconds have expired, whichever comes first.

How do you stop a script if conditions are met MATLAB?

Accepted Answer "return" will terminate the current function and go back to the function that calls it.


2 Answers

You can halt execution and give a command prompt in two ways of which I am aware:

  • Putting keyboard in your code where you want to stop.
  • Setting a breakpoint.

You can resume and stop execution with dbcont and dbquit, respectively. To step forward, use dbstep. dbstack lets you see where you are. There are many more commands. The help page for any of these will give you other suggestions.

As Dennis Jaheruddin has pointed out, dbstop also has several useful features worth trying. In particular is the ability to set conditional and global (any line meeting a criterion) breakpoints via the dbstop if syntax. For example, dbstop if error will break to a debugging command prompt on any error. One suggestion he made, which I now do, is to put dbstop if error into startup.m so that this behavior will be default when you start MATLAB. You may need to create this file in a userpath folder; edit(fullfile(regexp(userpath,'^[^;]*','match','once'),'startup.m')).

like image 81
chappjc Avatar answered Sep 22 '22 04:09

chappjc


One way to achieve what you're looking for would be to use code sections (also known as code cells), where you divide your code into sections divided by lines with two percent signs (%%).

Then, in the editor, you can press ctrl+enter to execute the current code section, and ctrl+up/down to navigate between sections.

like image 23
Junuxx Avatar answered Sep 25 '22 04:09

Junuxx