Examine Values While Debugging

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.

Select Workspace

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 dbstack function in the Command Window.

To select or change the workspace for the variable you want to view, use either of these methods:

  • From the Editor tab, in the Debug section, choose a workspace from the Function Call Stack menu list.

  • From the Command Window, use the dbup and dbdown functions to select the previous or next workspace in the Function Call Stack.

    To list the variables in the current workspace, use who or whos.

View Variable Value

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.

    Note

    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.

    1. On the Home tab, in the Environment section, click Preferences. Then select MATLAB > Editor/Debugger > Display.

    2. 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 n, type n and 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. 
The error occurs whether you select the parent workspace by using the dbup command or by using Function Call Stack field in the Debug section of the Editor tab.

Related Topics