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 an M-file to correct a problem or if you want to run other functions in MATLAB.
Note Always 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. |
To end debugging, click the exit debug mode icon , or select Exit Debug Mode from the Debug menu.
The function that ends debugging is dbquit
.
After quitting debugging, the pause indicators in the Editor/Debugger display no longer appear, and the normal prompt >>
now 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 by:
clear
Clear the breakpoints if you want the program to run uninterrupted, such as after identifying and correcting a problem. To clear a breakpoint in the Editor/Debugger, click on 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
dbclear all in collatzplot
Correcting an M-File. To correct a problem in an M-file:
Do not make changes to an M-file while MATLAB is in debug mode. It could produce unexpected debugging results when you run the M-file.
The breakpoints become unreliable once the M-file is edited. The breakpoints will produce unexpected debugging results when you run the file.
Completing the Example. To correct the problem in the example, do the following:
collatzplot.m
. One way to do this is by typingcollatzplot.m
line 11, change the string plot_seq
to seq_length(m)
and save the file.collatzplot
for n = 3 by typingn
= 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
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 7, 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 | ![]() |