You can debug your MATLAB® program interactively in the Editor or programmatically using debugging functions in the Command Window. Both methods are interchangeable. To debug a program in the Live Editor or in the Editor in MATLAB Online™, see Debug Code in the Live Editor.
Before you begin debugging, make sure that your program is saved and that the program and any files it calls exist on your search path or in the current folder.
If you run a file with unsaved changes from within the Editor, then the file is automatically saved before it runs.
If you run a file with unsaved changes from the Command Window, then MATLAB software runs the saved version of the file. Therefore, you do not see the results of your changes.
Set breakpoints to pause the execution of a MATLAB file so you can examine the value or variables where you think a problem could be. You can set breakpoints using the Editor, using functions in the Command Window, or both.
There are three different types of breakpoints: standard, conditional, and error. To add a standard breakpoint in the Editor, click the breakpoint alley at an executable line where you want to set the breakpoint. The breakpoint alley is the narrow column on the left side of the Editor, to the right of the line number. You also can use the F12 key to set the breakpoint.
Executable lines are indicated by a dash ( — ) in the breakpoint alley. For example, click the breakpoint alley next to line 2 in the code below to add a breakpoint at that line.
If an executable statement spans multiple lines, you can set a breakpoint at each line in that statement, even though the additional lines do not have a — (dash) in the breakpoint alley. For example, in this code. you can set a breakpoint at all four lines:
For more information on the different types of breakpoints, see Set Breakpoints.
After setting breakpoints, run the file from the Command Window or the Editor. Running the file produces these results:
The Run button changes to a Pause button.
The prompt in the Command Window changes to
indicating that MATLAB is in debug mode and that the keyboard is in control.
MATLAB pauses at the first breakpoint in the program. In the Editor,
a green arrow just to the right of the breakpoint indicates the pause. The
program does not execute the line where the pause occurs until it resumes
running. For example, here the debugger pauses before the program executes
x = ones(1,10);.
MATLAB displays the current workspace in the Function Call Stack, on the Editor tab in the Debug section.
If you use debugging functions from the Command Window, use
dbstack to view the
Function Call Stack.
For more information on using the Function Call Stack, see Select Workspace
To pause the execution of a program while it is running, go to the Editor tab and click the Pause button. MATLAB pauses execution at the next executable line, and the Pause button changes to a Continue button. To continue execution, press the Continue button.
Pausing is useful if you want to check on the progress of a long running program to ensure that it is running as expected.
Clicking the pause button can cause MATLAB to pause in a file outside your own program file. Pressing the Continue button resumes normal execution without changing the results of the file.
While your code is paused, you can view or change the values of variables, or you can modify the code.
View the value of a variable while debugging to see whether a line of code has produced the expected result or not. To do this, position your mouse pointer to the left of the variable. The current value of the variable appears in a data tip.
The data tip stays in view until you move the pointer. If you have trouble getting the data tip to appear, click the line containing the variable, and then move the pointer next to the variable. For more information, see Examine Values While Debugging.
You can change the value of a variable while debugging to see if the new value produces expected results. With the program paused, assign a new value to the variable in the Command Window, Workspace browser, or Variables Editor. Then, continue running or stepping through the program.
For example, here MATLAB is paused inside a
for loop where
n = 7; in the command line to change the
current value of
n from 2 to 7.
Press Continue to run the next line of code.
MATLAB runs the code line
x(n) = 2 * x(n-1); with
n = 7.
You can modify a section of code while debugging to test possible fixes without having to save your changes. Usually, it is a good practice to modify a MATLAB file after you quit debugging, and then save the modification and run the file. Otherwise, you might get unexpected results. However, there are situations where you want to experiment during debugging.
To modify a program while debugging:
While your code is paused, modify a part of the file that has not yet run.
Breakpoints turn gray, indicating they are invalid.
Select all the code after the line at which MATLAB is paused, right-click, and then select Evaluate Selection from the context menu.
After the code evaluation is complete, stop debugging and save or undo any changes made before continuing the debugging process.
While debugging, you can step through a MATLAB file, pausing at points where you want to examine values.
This table describes available debugging actions and the different methods you can use to execute them.
Continue execution of file until the line where the cursor is positioned. Also available on the context menu.
Run to Cursor
Execute the current line of the file.
Execute the current line of the file and, if the line is a call to another function, step into that function.
|Resume execution of file until completion or until another breakpoint is encountered.|
After stepping in, run the rest of the called function or local function, leave the called function, and pause.
Pause debug mode.
Exit debug mode.
After you identify a problem, end the debugging session by going to the Editor tab and clicking Quit Debugging . You must end a debugging session if you want to change and save a file, or if you want to run other programs in MATLAB.
After you quit debugging, pause indicators in the Editor display no longer appear,
and the normal
>> prompt reappears in the Command Window in
place of the
K>>. You no longer can access the call
If MATLAB software becomes nonresponsive when it pauses at a breakpoint, press Ctrl+c to return to the MATLAB prompt.