Main Content

Examine Values While Debugging

Since R2021b. Replaces Examine Value While Debugging (R2021a).

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:

  • Workspace browser — The Workspace browser displays all variables in the current workspace. The Value column of the Workspace browser shows the current value of the variable.

    Workspace browser showing two variables, n and x, with the current value for each variable displayed in the Value column

    To view more details, double-click the variable. The Variables Editor opens, displaying the content for that variable. You also can use the openvar function to open a variable in the Variables Editor.

  • Editor and Live Editor — To view the value of a variable in the Editor and Live Editor, place your cursor over the variable. The current value of the variable appears in a data tip. The data tip stays in view until you move the cursor. 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.

    Script with a data tip showing the value of the variable n

    Data tips are always enabled when debugging in the Editor. To disable data tips in the Live Editor or when editing a file in the Editor, go to the View tab and click the Datatips button off.

    You also can view the value of a variable or equation by selecting it in the Editor and Live Editor, right-clicking, and selecting Evaluate Selection in Command Window. MATLAB displays the value of the variable or equation in the Command Window.


    You cannot evaluate a selection while MATLAB is busy, for example, running a file.

  • Command Window — To view the value of a variable in the Command Window, type the variable name. For the example, to see the value of a variable n, type n and press Enter. The Command Window displays the variable name and its value. To view all the variables in the current workspace, call the who function.

View Variable Value Outside Current Workspace

When you are debugging a function or when you step into a called function or file, MATLAB displays the list of the functions it executed before pausing at the current line. The list, also called the function call stack, is shown at the top of the file in the Editor or Live Editor. The list 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.

Function call stack for plotRand showing plotRand as the first called script and mean as the current function

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 a different workspace. In the Editor or Live Editor, select a workspace from the drop-down list to the right of the function call stack at the top of the file.

Function call stack with the drop-down list of workspaces for the plotRand script open

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 who or whos.

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.

Related Topics