Debug MuPAD Code Using the Debugger

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


Besides syntax errors such as misspelling a function name or omitting parenthesis, run-time errors can appear when executing your code. For example, you might modify the wrong variable or code a calculation incorrectly. Runtime errors are usually apparent when your code produces unexpected results. Debugging is the process of isolating and fixing these run-time problems.

MuPAD® provides a tool to help you with the debugging process. With the Debugger, you can:

Open the Debugger

To open the Debugger window from a notebook:

  1. Select Notebook>Debug from the main menu.

  2. In the Debug Procedure Call dialog box enter the procedure call you want to debug.

  3. Click OK to open the Debugger for this procedure call.

You also can open the Debugger directly from the command line using the debug command, for example:


If you debug several lines of code, place the debug command in a separate input region. This allows you to avoid reevaluating the code every time you open the Debugger:

g := proc(x) begin x/(x+1) end_proc:
f := proc(n) begin g(10)^n end_proc:

Debug Step-by-Step

Using the MuPAD Debugger, you can run a procedure step by step. Running a procedure step by step helps you isolate the errors in your code. To start the step-by-step debugging process, select Step from the main menu.

Also you can use the toolbar buttons.

Executing your code step by step you can:

  • Use Step Over to execute the current line. If the code line contains a call to another function, the Debugger passes to the next code line without stepping into that function.

  • Use Step Into to execute the current code line and, if the code line contains a call to another function, the Debugger steps into that function.

  • After stepping in, use Step Out to run the rest of the current function, leave the called function, and pause.

For example, use Step Into to open and step through the inner procedure g.

Set and Remove Breakpoints

Set Standard Breakpoints

Set breakpoints to pause execution of your code so you can examine values where you think the problem is. To set a breakpoint inside a procedure:

  1. Select a line where you want to set a breakpoint.

  2. Select Breakpoints>Toggle Breakpoint from the main menu or right-click to use the context menu. Also you can click the Toggle Breakpoint button on the toolbar.

Set Conditional Breakpoints

To set a conditional breakpoint:

  1. Select a line where you want to set a breakpoint

  2. Select Breakpoints>Set Breakpoint from the main menu.

  3. In the Set Breakpoint dialog box, type the condition under which you want the Debugger to stop on this line. In the dialog box, you also can change the placement of a breakpoint defining the file and line where you want to set the breakpoint.

Use Breakpoints

After you set the breakpoint, you can continue the debugging process. The Debugger pauses at the breakpoints and executes the line with the breakpoint after you click the Continue button.

After setting breakpoints, you also can leave the current debugging session and start a new one. In the new session, the Debugger stops at all the breakpoints you previously set.

You can see the list of all breakpoints in the debugging process using the Breakpoints window. To open this window, select View>Breakpoints.

Remove Breakpoints

To remove a breakpoint:

  1. Select the breakpoint you want to remove.

  2. Select Breakpoints>Toggle Breakpoint from the main menu or right-click to use the context menu. Also, you can click the Toggle Breakpoint button on the toolbar. The second click releases the button and removes the breakpoint.

If you want to remove all breakpoints, select Breakpoints>Remove All Breakpoints from the main menu.

Evaluate Variables and Expressions After a Particular Function Call

During the debugging process you can check the values of variables by hovering the cursor over a particular variable. The Debugger displays the current value of the variable.

To evaluate the value of an expression:

  1. If you do not see the Output pane, select View>Output from the main menu.

  2. In the Output pane, type the expression you want to evaluate or select one of the prior expressions from the drop-down menu.

  3. Click the Evaluate button or press the Enter key.

Alternatively, you can select an expression and hover the cursor over it. If the expression is syntactically correct and can be computed fast, MuPAD displays the value of the expression in a tooltip.

Watch Intermediate Values of Variables and Expressions

You can observe the values of variables during the debugging process in the Watch pane. If you do not see the Watch pane, select View>Watch.

By default, the Debugger displays the values of the parameters used in the function call (args), the results of the most recent command (%), and the values of variables declared in the procedure. To watch the values of other variables and expressions during the debugging process, select Watches>Add Watch and enter the object or the expression you want to observe in the Add Watch dialog box.

You also can enter the expressions directly into the Watch table.

View Names of Currently Running Procedures

In the Call Stack pane, you can see the list of the procedures participating in the debugging process. If you do not see the Call Stack pane, select View>Call Stack.

The Debugger lists all the procedure calls in the Call Stack pane. The Debugger marks the name of the current procedure with a blue triangle and highlights the currently executed code line.

To switch between procedures, click the name of a procedure you want to switch to. Also, you can select Step>Stack Up or Step>Stack Down from the main menu or use the toolbar. As an alternative, you can press u and d. The Call Stack pane helps you navigate within nested calls of various procedures.

Correct Errors

The Debugger displays procedures and helps you find errors in the code, but you cannot correct the errors in the Debugger window. To edit your code use the MATLAB® Editor, a MuPAD notebook, or any text editor.

To open a new MATLAB Editor window, select File>New Editor with Source from the main menu or select Open in Editor from the context menu. The MATLAB Editor window contains the source that you see in the Debugger and lets you modify and save it.

Changes made in the Editor window do not automatically appear in the Debugger window. The Debugger presents the code that is already in the kernel. To run the Debugger on the corrected file:

  1. Close the Debugger window if it is open.

  2. In the Editor window select File>Save from the main menu to save changes.

  3. Open a notebook.

  4. Select Notebook>Read Commands from the main menu.

  5. Select the file you want to run.

  6. Start the Debugger from the notebook.