Debugging M-files
What is debugging in MATLAB?
Debugging is the process by which you isolate and fix errors in your program or
code.
2 kinds of errors:
● Syntax errors - For example omitting a parenthesis or misspelling a
function name.
● Run-time errors - Run-time errors are usually apparent and difficult to track
down. They produce unexpected results.
The above picture shows the ‘Debugger’ option available in the tool strip in Online
MATLAB.
This is how the sidebar appears when the debugger is enabled. It shows the
breakpoints and the line numbers where the breakpoints are set.
Debugging process
● Preparing for debugging
● Setting breakpoints
● Running an M-file with breakpoints
● Stepping through an M-file
● Examining values
● Correcting problems
● Ending debugging
Step 1: Preparing for debugging
Do the following to prepare for debugging:
● Open the file
● Save changes
● Be sure the file you run and any files it calls are in the directories that are on
the search path.
The above picture shows a script-file with a sample code written for which
breakpoints are to be set.
Step 2: Setting breakpoints
Breakpoints are specific spots in your code where you tell the program to pause
while it's running. They help you check what’s happening inside the program at
that exact point, like seeing the values of variables or figuring out why something
isn't working as expected. Once the program stops at a breakpoint, you can step
through the code line by line, fix errors, or understand how the code is behaving
before continuing.
There are three basic types of breakpoints:
● A standard breakpoint, which stops at a specified line.
● A conditional breakpoint, which stops at a specified line and under specified
conditions.
● An error breakpoint that stops when it produces the specified type of
warning, error, NaN, or infinite value.
To set up breakpoints in the code, we can go to the command window and type the
following code:
dbstop in <filename> at <line number>
Example: If we want to set breakpoint to the ‘if’ condition as a conditional
breakpoint, it can be done by mentioning the line number in command window as
follows:
The breakpoint is now set to line number 6. Also we will be able to find a red
square marking line number 6 in the code which indicates that a breakpoint is
being set.
The red square indicates a breakpoint in that line
The breakpoints also appear on the sidebar as well once being set in the code.
Step 3: Running with breakpoints
After setting breakpoints, run the M-file from the Editor/Debugger or from the
Command Window. Running the M-file results in the following:
● The prompt in the Command Window changes to ‘K>>’ indicating that
MATLAB is in debug mode.
● 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.
● In breakpoint, we can examine variables, step through programs, and run
other calling functions.
The toolstrip has an option called ‘Run’. Clicking on it will run the code. But the
code will stop executing/running when it reaches the breakpoint and the command
prompt will look as shown below:
The green arrow indicates that the execution is stopped at that line because of the
breakpoint being set there.
This line will be executed only on pressing the ‘Continue’ option available in the
toolstrip.
Step 4: Examining values
While the program is paused, we can view the value of any variable currently in
the workspace. Examine values when we want 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, and continue running. If the result is not as expected, then that line, or the
previous line, contains an error. Viewing values as datatips: First, we position the
cursor to the left of a variable on that line. Its current value appears. This is called a
datatip, which is like a tooltip for data.
Placing the cursor near the variable will display the value that the variable holds
currently. This is called ‘datatips’ as bringing the cursor tip shows the value.
The value of the variable can also be seen in the Workspace.
Step 5: Correcting and ending debugging
While debugging, we can change the value of a variable 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. Then continue running and stepping through
the program.
To check what value the variable holds, we can type the variable name in the
command window and press enter. The current value of the variable will be
displayed there.
Step 6: Ending debugging
After identifying a problem, end the debugging session. It is best to quit debug
mode before editing an M-file. Otherwise, you can get unexpected results when
you run the file. To end debugging, select ‘Exit Debug Mode’ from the Debug
menu.
On right-clicking the line number where the breakpoint is set, we can see various
debugging options. Of those, click on ‘Clear Breakpoint’ to remove the breakpoint
that is set.
Or another method will be typing the following command in the command window
we can remove the breakpoint:
dbclear in <filename> at <line number>
The above image shows the command typed to remove the breakpoint that was set
at line number 6. Now, the breakpoint has been removed and here’s how the code
looks like:
Now there will not be any red squares or green arrows as all breakpoints have been
removed.
Step 7: Correcting an M-file
To correct errors in an M-file,
● Quit debugging
● Do not make changes to an M-file while MATLAB is in debug mode
● Make changes to the M-file
● Save the M-file
● Clear breakpoints
● Run the M-file again to be sure it produces the expected results
Now after clearing all breakpoints and exiting the debugger mode, the code will
execute as per the flow and we can ensure that the code produces the expected
results.