To set breakpoints with the command line, dbstop
is the tool (plus dbclear
to clear breakpoints and dbstatus
to list them).
There are presently 17 different forms to dbstop
, which allow you to specify various combinations of:
- The M-file in which to stop
- Line number
- Sub-function
Conditional to an arbitrary expression. For example,
dbstop in myFun.m at 224 if ~exist('x','var')
- At any run-time error (
dbstop if error
)
- At a specific error (e.g
dbstop if error myFun.m:barErrorId
)
- At any warning (
dbstop if warning
) or specific warning
- If
NaN
or Inf
are encountered (dbstop if naninf
)
See the documentation for dbstop
for details and good examples.
Also get used to dbcont
(or F5), dbstep
(or F10), dbquit
(Shift+F5), dbstep
(also dbstep in
, dbstep out
), dbstack
(to see where you are and how you got there). The keyboard shortcuts may be different outside of Windows.
Far less used, but still very useful are dbup
and dbdown
, which allow you to switch workspace context (memory stacks).
See the summary of functions and a list of examples and how-to pages in the MathWorks page on Debugging.
Related to the "db
" functions is checkcode
, which will check your code for possible problems before you even run it. This is a nice substitute for the red squiggly underlines that you would get in the MATLAB Editor.
Once you get a hang of dbstop
and it's syntax, you won't often need to insert a keyboard
into your code, but it's always an option.