MATLAB Answers

How can I "watch" variables using the MATLAB Debugger?

99 views (last 30 days)
I would like to set "watches" on variables while using the MATLAB Debugger. In particular, I would like to configure the debugger to break when the variable reaches a certain value or exceeds a certain range.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Jul 2020 at 4:00
Edited: MathWorks Support Team on 10 Jul 2020 at 10:49
In order to "watch" a variable, you can set a conditional breakpoint to stop when a certain condition is met. You can select a conditional break point interactively from the editor tab in MATLAB R2012b or newer, or through the command line.
For example, to stop in debug mode when ii is equal to 25, in the file "watched.m", which consists of the following:
for ii = 1:100
disp(ii)
end
You could run:
dbstop in watched at 2 if (ii == 25)
For more information, please see the documentation for dbstop:

  2 Comments

Rod Macpherson
Rod Macpherson on 31 Dec 2015
How do you set a watch? That is, not a conditional breakpoint but a watch variable.
UbuntuXenial
UbuntuXenial on 4 Apr 2019
Did you ever find any answer to this? If so, can you please share?

Sign in to comment.

More Answers (2)

Marshall
Marshall on 28 Apr 2014
You can set conditional breakpoints to stop when a variable meets some condition that you specify: Where breakpoints are shown right click and select "set conditional breakpoint" or right click on an existing breakpoint and choose "set/modify condition". Then type an expression that will be evaluated to trigger the breakpoint, e.g. a>100.

  0 Comments

Sign in to comment.


Darin Williams
Darin Williams on 2 Nov 2015
With some extra tools (from Darin_dbtools on Matlab file exchange), you can also use conditional breakpoints to do a lot more than stop at a particular value. You can place code in the "conditional" breakpoint to look at SELECTED variables as your code runs (dbdisp), change variables (dbassign), or to call user code (dbcall). And, you can CHANGE any of this within the debugger as you code continues to run!
For example, if you place a conditional breakpoint with the condition: "dbdisp([x,y])", your code will act as though disp([x,y]) has been inserted in front of that line until you disable the breakpoint. Since these routines always return false, the code will not stop.
The combination of break_place.m and break_place_button.m provides a mechanism other than break points to interactively interrupt running code, causing it to drop into the debugger on a mouse click. For example, you can have a days-long loop running unattended, interrupt it by clicking on a button, look at things in the debugger, then continue as though nothing had happened. It isn't 100% solid across Matlab configurations, but has still saved me hundreds of hours of debug time.
Darin_dbtools_demo gives further examples.
Hope that helps.

  0 Comments

Sign in to comment.

Tags

Products


Translated by