Development Environment |
Correcting Problems and Ending Debugging
These are some of the ways to correct problems and end the debugging session:
Many of these features are used in Completing the Example.
Changing Values and Checking Results. While debugging, you can change the value of a variable in the current workspace to see if the new value produces expected results. While the program is paused, assign a new value to the variable in the Command Window or in the Array Editor. Then continue running or stepping through the program. If the new value does not produce the expected results, the program has a different or another problem.
Ending Debugging. After identifying a problem, end the debugging session. You must end a debugging session if you want to change and save an M-file to correct a problem or if you want to run other functions in MATLAB.
Note It is best to quit debug mode before editing an M-file. If you edit an M-file while in debug mode, you can get unexpected results when you run the file. If you do edit an M-file while in debug mode, breakpoints turn gray, indicating that results might not be reliable. See Valid (Red) and Invalid (Gray) Breakpoints for details. |
To end debugging, click the exit debug mode icon , or select Exit Debug Mode from the Debug menu.
You can instead use the function dbquit
to end debugging.
After quitting debugging, the pause indicators in the Editor/Debugger display no longer appear, and the normal prompt >>
appears in the Command Window instead of the debugging prompt, K>>
. You can no longer access the call stack.
Clearing Breakpoints. Breakpoints remain in a file until you clear them or until they are automatically cleared.
Clear the breakpoints if you want the program to run uninterrupted, for example, after identifying and correcting a problem.
To clear a breakpoint in the Editor/Debugger, click the breakpoint icon for a line, or select Set/Clear Breakpoint from the Breakpoints or context menu. The breakpoint for that line is cleared.
To clear all breakpoints in all files, select Clear All Breakpoints from the Breakpoints menu, or click the equivalent button on the toolbar.
The function that clears breakpoints is dbclear
. To clear all breakpoints, use dbclear all
. For the example, clear all of the breakpoints in collatzplot
by typing
Breakpoints are automatically cleared when you
Correcting an M-File. To correct a problem in an M-file:
Completing the Example. To correct the problem in the example, do the following:
collatzplot.m
line 12, change the string plot_seq
to seq_length(m)
and save the file.
collatzplot.m
. One way to do this is by typing
n
= 1, 2 when n
= 2, and 8 when n
= 3, as expected.
n
, such as 6, to be sure the results are still accurate. To make it easier to verify collatzplot
for n
= 6 as well as the results for collatz
, add this line at the end of collatz.m
sequence
which displays the series in the Command Window. The results for when n = 6
are
collatzplot
for a small value of n
. Now that you know it works correctly, run collatzplot
for a larger value to produce more interesting results. Before doing so, you might want to suppress output for the line you just added in step 6, line 19 of collatz.m
, by adding a semicolon to the end of the line so it appears as
Examining Values | Preferences for the Editor/Debugger |