Can you pause MATLAB? [duplicate]

不想你离开。 提交于 2019-12-03 14:54:03

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

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!