Debug a MATLAB Program

To debug your MATLAB® program graphically, use the Editor/Debugger. Alternatively, you can use debugging functions in the Command Window. Both methods are interchangeable.

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 Breakpoint

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. 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.

After setting breakpoints, run the file from the Command Window or the Editor. Running the file produces these results:

  • The prompt in the Command Window changes to K>> 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, located 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.

    Tip   To debug a program, run the entire file. MATLAB does not stop at breakpoints when you run an individual section.

For more information on the different types of breakpoints, see Set Breakpoints. For more information on using the Function Call Stack, see Select Workspace

Find and Fix a Problem

While your code is paused at a breakpoint, you can view or change the values of variables, or modify the code.

View or Change Variable While Debugging

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 variable's current value 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 = 2:

  • Type 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.

Modify Section of Code While Debugging

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:

  1. While stopped at a breakpoint, modify a part of the file that has not yet run.

    Breakpoints turn gray, indicating they are invalid.

  2. Select all of the code after the breakpoint, 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.

Step Through File

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.


Toolbar Button

Debug Menu Item

Function Alternative

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.

Step In

dbstep in

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.

Step Out

dbstep out

Exit debug mode.

Quit Debugging


End Debugging Session

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 stack.

MATLAB software can become nonresponsive if it stops at a breakpoint while displaying a modal dialog box or figure that your file creates. In that event, press Ctrl+C to go to the MATLAB prompt.

Related Examples

Was this topic helpful?