MATLAB: How to “watch” variables using the MATLAB Debugger

MATLABwatchpoint

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.

Best Answer

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: