Examples

    1. Copy these files to your current working directory: fff.m and example1.m
    2. Set a break point at line 2 in the example1.m file.
    3. Click the run button from the editor window.

    The editor window should now look like this.

    execution stopped at breakpoint

    Notice that the debugger operations are all available. First, we will simply step through this script one line at a time. Dock the editor window and make sure that the workspace and command windows are showing before continuing with this example.

    Click on the Step button and watch what happens in the workspace window. After executing the clear command, the workspace is cleared of all variables.

    Click on the Step button again and see that the clc command clears the command window.

    Click on the Step button again to execute the statement that creates the xx vector. View the results in the workspace window.

    The editor looks like this now.

    execution stopped after xx exists in workspace

    Click on the Step button to execute line 7, the line that creates the yy vector by calling the ff function. The debugger stops and an error is displayed in the command window. The next screen shot shows the error that occurs and is shown in the command window.

    Undefined command/function 'ff' error
    1. Fix the error by typing the correct name of the function fff.
    2. Remove the earlier breakpoint (at line 2) by clicking on the red dot.
    3. Set a new breakpoint at line 7 since we know the earlier lines are working.
    4. Click the Run button to start the program again.

    The editor window should now look like this with the execution stopped at the breakpoint on line 7.

    breakpoint at line 7

    Click on the Step in button instead of the Step button. Notice that the file fff.m is now showing in the editor window and the green execution arrow is indicating the first line of the function.

    execution error at start of function fff.m

    Click the Step button to execute the first line of the function. This line creates a copy of the input value with a more convenient name.

    Click the Step button to execute the last line of the function and view the results. Notice the green indicator arrow at the end of the function.

    execution arrow pointing down at end of the function fff.m

    Click the Step button to execute the last line of the function and view the results. Notice the errors that occur when the function returns. The main problem is listed first. This error indicates that the named return value, f_value, of the function was not assigned to.

    errors in command window
    1. Fix the error by changing line 3 of the function fff.m to:
          f_value = x./(1+x.^2/4)-1/2;
    2. Save the fff.m file.
    3. Click on the example1.m file in the editor window.
    4. Click on the Run button to start executing the program again.
    5. Click on the Step in button to enter the function again.
    6. Click on the Step button to execute each line of the function.
    7. Click on the Step button to execute the return from the function.

    Control returns successfully to the example1.m program script. View the workspace window to see that the variable yy has been created successfully.

    Click on the Step button to execute the last line of the program.

    One more error occurs.

    1. Change ff to yy to fix this last error.
    2. Save the file.
    3. Remove the breakpoint at line 7
    4. Set a breakpoint at line 8
    5. Click on the Run button to start the program again.

    Click on the Step button to execute the plot command and see the plot of the function named fff.

    This tutorial has walked you through some common steps for debugging a program. Debugging in this way is most effective when you have found the few lines in which the error is occurring.

    If you have no idea where to set a breakpoint to get started debugging, it is often good to remove any output suppression characters (;) and run the program. This will allow the programmer to see where the output is not as expected. Set breakpoints a few lines before the incorrect output occurs and then step through each line of the program's execution path from that point on.