In today’s section we will cover the following topics:
1. Debugging (including Breakpoints and Stepping)
2. The Pause Command
VIDEO COMING SOON!
Debugging (including Breakpoints and Stepping)
MATLAB has built-in debugging tools. Debugging means to find errors in the code so it can run properly. Debugging helps to fix two different types of errors:
(a) Syntax Errors
This is a misspelling in the syntax (recall syntax just refers to the words of your code)
(b) Run-time Errors
These are errors that are difficult to track down, and they produce unexpected results because it isn’t due to any incorrect spelling/syntax. These usually happen when the program already executes but the results are not what you expect. Whereas if you have syntax errors then your code likely won’t run at all until they’re fixed.
There are two different ways to debug on MATLAB. These are interchangeable:
(a) Use the Editor/Debugger
(b) Use the debugging functions in the Command Window.
I will cover how to debug using the Editor/Debugger (a), and then at the very end of this lesson, I have a table of alternative commands to use instead (b)
The steps to debugging are the following:
(a) Preparing for Debugging by Setting Breakpoints
You should first set breakpoints in your code. Breakpoints are just points in the code where you want MATLAB to pause execution of the code, so it won’t run the next line of code if you add a breakpoint at a certain line.
This is helpful because you can examine whether the program is running ok at each break, or whether there is a problem anywhere. It would be more difficult to have to examine the entire code for problems once a long script is finished running rather than to set breakpoints to begin with.
There are three types of breakpoints:
(1) Standard Breakpoint
This stops the code at a specified line, so you can check for any errors or see the results at that point in the code. To add a standard breakpoint, click the breakpoint alley at the line you want to set the breakpoint at. The breakpoint alley is shown in the image below with the blue arrow. You can set a breakpoint at any of the lines, which are indicated in the breakpoint alley with a dash (-).
Example – Setting Standard Breakpoint
I have set a breakpoint at Line 2.
Note here that if one statement spans two or more lines, the breakpoint dash will only be displayed on the first line, but you can still set a breakpoint on the next line. Here, I have separated the if statement from Line 3, to Lines 3 and 4.
Notice how Line 4 does not have a dash next to it, but I can now set a breakpoint there:
(2) Conditional Breakpoint
This stops the code at a specified line and under specified conditions. For example, this breakpoint could be used within a loop only if the specified condition is met. This is helpful if you want to check the results of your code after iterations in a loop.
To set a conditional breakpoint, follow the same steps to set a regular breakpoint, but when you set the red dot for the breakpoint, select Set/Modify Condition by right-clicking it instead. At the Editor dialog box pop-up, enter a condition you want to specify and hit OK. This will be evaluated before running the code. This can be a condition that is not within your code already.
Example – Setting Conditional Breakpoint
Say we have the following loop:
If you try to set a breakpoint at Line 3 and then right-click it, this is what comes up:
When you select Set/Modify Conditions, this box pops up. You can set the condition that you want MATLAB to pause on during execution if the condition is true:
Now when you run the code, if that condition is true, MATLAB will pause at that breakpoint in the Command Window.
(3) Error Breakpoint
This stops the code when it produces a specified type of error/warning, NaN (not a number), or an infinite value. These are not breakpoints you can set at specific lines like the other two were, but rather you will set this breakpoint overall within MATLAB. It will then pause at any line within any file if the specified error condition occurs.
To set an error breakpoint, click on the Editor tab, then select Breakpoints. Click on More Error and Warning Handling Options at the bottom.
Select one of the four options to specify the error condition on which MATLAB should pause. The main ones I would worry about are Errors (pauses on any errors), Warnings (pauses on any warnings), or NaN or Inf (pauses when you have Nan or Inf values):
Once you select the correct setup, hit OK. Now when you run your code and have an error or warning, MATLAB will pause at the specific condition you had chosen.
(b) Running an M-File with Breakpoints
Once the breakpoints are set, run the M-File from the Editor/Debugger or from the Command Window.
Running the M-File will cause the following changes:
(a) The prompt in the Command Window changes to
K>>, indicating that MATLAB is in debug mode. This is what it looks like:
(b) The program pauses at the first breakpoint. This means that line will be executed when you continue. The pause is indicated by the green arrow, as shown below next to the red breakpoint.
(c) Examining Values
While the program is paused, we can view the value of any variable currently in the script. Examine these values to see whether a line of code has produced the expected result or not.
If the result is as expected, step to the next line (as one of the screenshots above showed the Step button at the top of the MATLAB window), and continue running. If the result is not as expected, then that means that that specific line, or the previous line, contains an error.
To do this, we will view values as datatips
A datatip is a text box that appears showing the name and current value of a variable (aka a data point). To see the datatip, position the cursor to the left of a variable on the value you want to view, in your script or function. If you have trouble getting the datatip to appear, click in the line and then move the cursor next to the variable.
In this script, I have placed my cursor over the variable Y2, and the values I have stored in this variable (a vector) come up. You can do this while your program is paused at a line of code to see whether you are getting the expected result:
Whos and Who commands
The who or whos commands list out the variables in the Workspace in different ways.
Here I have typed whos in the Command Window, and all the variables in my Workspace, with their properties, show up:
Now I have typed who in the Command Window, and just the variables come up:
(d) Correcting Problems
While debugging, we can change the value of a variable to see if the new value produces expected results – for example, if you noticed that you were not getting the expected result when you checked the output of your code during a breakpoint, you can can change the variable right there rather than letting the entire program execute and then changing it. While the program is paused, you can assign a new value to the variable by typing it into the Command Window.
Then continue running or stepping through the program. In the image below, the Continue button at the top will continue running through the program, and the Step button will step through each line.
(e) Ending Debugging
To correct errors you’ve found using the debugging tool, do not edit anything while the debug mode is on. Quit the debugging tool by selecting Quit Debugging from the top bar, then make changes to the file, save it, and clear breakpoints before running again. You can clear breakpoints by clicking the same place as you did to create them, which was discussed above.
See below where the Quit Debugging button is located:
The Pause Command
The pause command is useful when a program is executing/running but you want to pause it while it does this. For example, this can help when a program is quite long and you want to be able to check if the program output is giving the expected results.
To pause a program that’s running, go to the Editor tab on the top of the program and click the Pause button.
MATLAB will pause running the program at the next line of code, and the Pause button will then change to a Continue button. Then when you want MATLAB to continue running the program, hit Continue.
Alternative Commands
Here is a table of function alternatives to each of the commands discussed above. Instead of using the buttons on the top bar, you can instead type these commands in the Command Window to get MATLAB to do the same thing: