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

Main Content

Since R2021b. Replaces Examine Value While Debugging (R2021a) (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:

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.