Examine Values While Debugging - MATLAB & Simulink (original) (raw)

Main Content

When debugging a code file, you can view the value of any variable currently in the workspace while MATLAB® is paused. If you want to determine whether a line of code produces the expected result or not, examining values is useful. If the result is as expected, you can continue running the code or step to the next line. If the result is not as you expect, then that line, or a previous line, might contain an error.

View Variable Value

There are several ways to view the value of a variable while debugging:

View Variable Value Outside Current Workspace

When you are debugging a function or when you step into a called function or file, the Debugger panel displays the list of the functions it executed before pausing at the current line in the Function call stack section. The list, also called the function call stack, displays the functions in order, with the current script or function in which MATLAB is paused at the top of the list, and the first called script or function at the bottom of the list. (since R2025a)

To open the Debugger panel if it is not open, go to theEditor or Live Editor tab, and in theAnalyze section, click Debugger. You also can open the panel using the Open more panels button on a sidebar.

Debugger panel with the Function call stack section showing that the debugger is paused at line 3 of plotRand.m

Before R2025a: The function call stack is shown at the top of the file and displays the functions in order, starting on the left with the first called script or function, and ending on the right with the current script or function in which MATLAB is paused.

You also can use the dbstack function to view the current workspace in the Command Window:

In mean (line 48) In plotRand (line 5)

For each function in the function call stack, there is a corresponding workspace. Workspaces contain variables that you create within MATLAB or import from data files or other programs. Variables that you assign through the Command Window or create by using scripts belong to the base workspace. Variables that you create in a function belong to their own function workspace. To examine the values of variables outside of the current workspace, select the corresponding function in the Debugger panel Function call stack section.

You also can use the dbup and dbdown functions in the Command Window to select the previous or next workspace in the function call stack. To list the variables in the current workspace, use the who or whos functions.

If you attempt to view the value of a variable in a different workspace while MATLAB is in the process of overwriting it, MATLAB displays an error in the Command Window.

K>> x Variable "x" is inaccessible. When a variable appears on both sides of an assignment statement, the variable may become temporarily unavailable during processing.

The error occurs whether you select the workspace by using the drop-down list to the right of the function call stack or the dbup command.

See Also

Topics