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.

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 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

Correcting an M-File.   To correct a problem in an M-file:

  1. Quit debugging.

    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.

  1. Clear all the breakpoints in the file.

    The breakpoints become unreliable once the M-file is edited. The breakpoints will produce unexpected debugging results when you run the file.

  1. Make changes to the M-file.
  2. Save the M-file.
  3. Set breakpoints, if desired.
  4. Run the M-file again to be sure it produces the expected results.

Completing the Example.   To correct the problem in the example, do the following:

  1. End the debugging session. One way to do this is to select Exit Debug Mode from the Debug menu.
  2. Clear the breakpoints in collatzplot.m. One way to do this is by typing

    dbclear all in collatzplot

    in the Command Window.

  1. In collatzplot.m line 11, change the string plot_seq to seq_length(m) and save the file.
  2. Run collatzplot for n = 3 by typing

    collatzplot(3)

    in the Command Window.

  1. Verify the result. The figure shows that the length of the Collatz series is 1 when n = 1, 2 when n = 2, and 8 when n = 3, as expected.

  2. Test the function for a slightly larger value of 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.

    Then run collatzplot for n = 6 by typing

    collatzplot(6)

  1. To make debugging easier, you ran 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

    sequence;

    Then run

    collatzplot(500)

    The following figure shows the lengths of the Collatz series for n = 1 through n = 500.


 Examining Values Preferences for the Editor/Debugger