8

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.

Amro
  • 123,847
  • 25
  • 243
  • 454
Shen
  • 164
  • 1
  • 1
  • 7
  • 1
    You may be interested in reading more about the debugging functions in matlab - http://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html#brqxeeu-184 – marsei Sep 17 '13 at 20:41
  • perhaps this approach might help if you want to interactively pause the code, enter debugging mode, inspect variables, then continue execution: http://stackoverflow.com/a/3273167/97160 – Amro Sep 19 '13 at 01:57

4 Answers4

6

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')).

chappjc
  • 30,359
  • 6
  • 75
  • 132
  • 1
    Note that breakpoints allow you to set conditions (just rightclick on one). This allows you to easily stop every 100th iteration for example, or when a certain condition is met. Also look into `help dbstop`. – Dennis Jaheruddin Sep 18 '13 at 08:05
2

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.

Junuxx
  • 14,011
  • 5
  • 41
  • 71
  • This is not incorrect. Why the down vote? Same for my answer for that matter. – chappjc Sep 17 '13 at 20:44
  • @chappjc: Well, Andreas does have a point that with `pause`, you can't check the workspace variables. For me that alone wouldn't be a reason for a downvote though, and your solution doesn't even have that problem. – Junuxx Sep 17 '13 at 20:47
  • 1
    Removed the `pause` suggestion since it didn't fit OP's needs, and expanded on the code sections explanation. – Junuxx Sep 17 '13 at 21:11
1

Well there is the pause command, but then you cannot check for the variable contents in the workspace because the program is running.

What you probably want is to set a breakpoint (See the Debug menu / key F12).

At a breakpoint matlab pauses the program and enters debugging mode in which you can see and edit the variables. Once finished, you can resume the program where it was paused.

Andreas H.
  • 5,557
  • 23
  • 32
  • You can insert a breakpoint using `keyboard` function without needing to use matlab IDE, which I don't like, btw. Nvm, chappjc already stated it x) – Werner Sep 17 '13 at 22:27
1

I'm not sure about Windows users but if you're running Linux you can start Matlab in a terminal using

matlab -nodesktop

then once Matlab has started, cd to your project directory and start your Matlab script. Now whenever you want to pause execution you can use ctrl-Z. Then to resume type fg. I hope this helps.