Development Environment | ![]() ![]() |
Setting Breakpoints
Set breakpoints to pause execution of the function so you can examine values where you think the problem might be. You can only set breakpoints at executable lines in saved files that are in the current directory or in directories on the search path. When you create a new M-file, save it before setting breakpoints. You cannot set breakpoints while MATLAB is busy, for example, running an M-file.
Breakpoints for the Example. It is unclear whether the problem in the example is in collatzplot
or collatz
. To start, set breakpoints in collatzplot.m
at lines 10, 11, and 12. The breakpoint at line 10 allows you to step into collatz
to see if the problem might be there. The breakpoints at lines 11 and 12 stop the program where you can examine the interim results.
Setting Breakpoints Using the Editor/Debugger. To set a breakpoint using the Editor/Debugger, click in the breakpoint alley at the line where you want to set the breakpoint. The breakpoint alley is the column to the right of the line number. You can only set breakpoints at lines that are preceded by a - (dash). Lines not preceded by a dash, such as comments, are not executable. Other ways to set a breakpoint are to position the cursor in the line and then click the set/clear breakpoint button on the toolbar, or select Set/Clear Breakpoint from the Breakpoints menu or the context menu.
A breakpoint icon appears, as in the following illustration for line 10.
Function Equivalent. To set a breakpoint using the debugging functions, use dbstop
. For the example, type
dbstop in collatzplot at 10 dbstop in collatzplot at 11 dbstop in collatzplot at 12
Some useful related functions are:
Setting Stops for Conditions. Use these items on the Breakpoints menu or the dbstop
function equivalents to instruct the program to stop when it encounters a problem:
dbstop if error
dbstop if warning
dbstop if naninf
or dbstop if infnan
![]() | Using Debugging Features | Running an M-File | ![]() |