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.

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:

  1. Quit debugging.
  1. Do not make changes to an M-file while MATLAB is in debug mode. 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.

  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. In collatzplot.m line 12, change the string plot_seq to seq_length(m) and save the file.
  3. Clear the breakpoints in collatzplot.m. One way to do this is by typing
  1. dbclear all in collatzplot

    in the Command Window.

  1. Run collatzplot for n = 3 by typing
  1. 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
  1. sequence

    which displays the series in the Command Window. The results for when n = 6 are

    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 6, line 19 of collatz.m, by adding a semicolon to the end of the line so it appears as
  1. 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