While your program is paused, you can view the value of any variable currently in the workspace. Examine values when you want to see whether a line of code produces the expected result or not. If the result is as expected, continue running 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.
To examine a variable during debugging, you must first select
its workspace. Variables that you assign through the Command Window
or create using scripts belong to the base workspace. Variables that
you create in a function belong to their own function workspace. To
view the current workspace, select the Editor tab.
The Function Call Stack field shows the current
workspace. Alternatively, you can use the
in the Command Window.
To select or change the workspace for the variable you want to view, use either of these methods:
There are several ways to view the value of a variable while debugging a program:
View variable values in the Workspace browser and Variables Editor.
The Workspace browser displays all variables in the current
workspace. The Value column of the
Workspace browser shows the current value of the variable. To see
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.
View variable values in the MATLAB® Editor.
Use your mouse to select the variable or equation. Right-click and select Evaluate Selection from the context menu. The Command Window displays the value of the variable or equation.
You cannot evaluate a selection while MATLAB is busy, for example, running a file.
View variable values as a data tip in the MATLAB Editor.
To do this, position your mouse pointer over the variable. The current value of the variable 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.
Data tips are always enabled when debugging a file in the Editor. To view data tips when editing a file in the Editor, enable them in your MATLAB preferences.
On the Home tab, in the Environment section, click Preferences. Then select MATLAB > Editor/Debugger > Display.
Under General display options, select Enable datatips in edit mode.
View variable values in the Command Window.
To see all the variables currently in the workspace, call the
who function. To view the current value
of a variable, type the variable name in the Command Window. For the
example, to see the value of a variable
press Enter. The Command Window displays the variable
name and its value.
When you set a breakpoint in a function and attempt to view the value of a variable in a parent workspace, the value of that variable might not be available. This error occurs when you attempt to access a variable while MATLAB is in the process of overwriting it. In such cases, MATLAB returns the following message, where x represents the variable whose value you are trying to examine.
K>> x Reference to a called function result under construction x.
dbupcommand or by using Function Call Stack field in the Debug section of the Editor tab.