Development Environment |
Set breakpoints to pause execution of the function so you can examine values where you think the problem might be. You can only set valid 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, unless that M-file is paused at a breakpoint.
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. Set breakpoints at lines that are preceded by a - (dash). Lines not preceded by a dash, such as comments or blank lines, are not executable--if you try to set a breakpoint there, it is actually set at the next executable line. 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 the example.
Valid (Red) and Invalid (Gray) Breakpoints. Red breakpoints are valid breakpoints. If breakpoints are instead gray, they are not enabled and therefore not valid.
Breakpoints are gray for either of these reasons:
Function Alternative. To set a breakpoint using the debugging functions, use dbstop
. For the example, type
Some useful related functions are
Setting Stops for Conditions. Use the items on the Breakpoints menu or the dbstop
function equivalents to instruct the program to stop when it encounters a problem. For details, see dbstop
.
dbstop if error
dbstop if warning
dbstop if naninf
or dbstop if infnan
dbstop if all error
If the File Is Not on the Search Path or in the Current Directory. When you add or remove a breakpoint in a file that is not in a directory on the search path or in the current directory, a dialog box appears, presenting you with options that allow you to add or remove the breakpoint. You can either change the current directory to the directory containing the file, or you can add the directory containing the file to the search path.
Using Debugging Features | Running an M-File with Breakpoints |