Development Environment    

Examining Values

While the program is paused, you can view the value of any variable currently in the workspace. Use the following methods to examine values:

Many of these methods are used in Examining Values in the Example.

Where to Examine Values.   When the program is paused, either at a breakpoint or at a line you have stepped to, you can examine values. Examine values when you want to see whether a line of code has produced 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 expected, then that line, or a previous line, contains an error.

In the example, because the results for n = 1 are correct, there is no need to examine values until n = 2. Click the continue button in collatzplot twice to move to line 10 of collatzplot again.

Selecting the Workspace.   Variables assigned through the Command Window and created using scripts are considered to be in the base workspace. Variables created in each function have their own workspace. To examine a variable, you must first select its workspace. When you run a program, the current workspace is shown in the Stack field. In the example, the workspace is currently collatzplot. To examine values that are part of another function workspace currently running or the base workspace, first select that workspace from the list in the Stack field. If you use debugging functions, use dbup and dbdown to change to a different workspace.

Viewing Values as Datatips in the Editor/Debugger.   In the Editor/Debugger, position the cursor to the left of a variable on that line. Its current value appears--this is called a datatip. It stays in view for a few seconds or until you move the cursor. If you have trouble getting the datatip to appear, click in the line and then move the cursor next to the variable.

In the example, step into collatz and then position the cursor over n in collatz--the datatip shows that n = 2, as expected. Note that the Stack shows collatz as the current function.

Viewing Values in the Command Window.   You can do this while in debug mode, at the K>> prompt. To see the variables currently in the workspace, use who. Type a variable name in the Command Window and MATLAB displays its current value. For the example, to see the value of n , type

and MATLAB returns the expected result

Viewing Values in the Array Editor.   You can view the value of any variable in the Array Editor. To view the current variables, open the Workspace browser. In the Workspace browser, double-click a variable. The Array Editor opens, displaying the value for that variable. You can also open the Array Editor for a variable using openvar.

To see the value of n in the Array Editor for the example, type

and the Array Editor opens, showing that n = 2 as expected.

Evaluating a Selection.   Select a variable or equation in an M-file in the Editor/Debugger. Right-click and select Evaluate Selection from the context menu. 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 an M-file.

Examining Values in the Example.   In collatz, use the step button or the function dbstep. The program advances to line 10, where there is no need to examine values. Continue stepping until line 13. When you step again, the pause indicator jumps to line 17, just after the if loop, as expected, given the code in line 13 for next_value = 2. When you step again to line 18, you can check the value of sequence in line 17 and see that it is 2 1 as expected for n = 2. Stepping again takes you from line 18 to line 11. At line 11, step again. Because next_value is 1, the while loop ends. The pause indicator is at line 11 and appears as a green down arrow . This indicates that processing in the called function is complete and program control will return to the calling program, in this case, collatzplot line 10. Step again from line 11 in collatz and execution is now paused at line 10 in collatzplot.

In collatzplot, step again to advance to line 11, then line 12. The variable seq_length in line 11 is a vector with the elements 1  2, which is correct.

Finally, step again to advance to line 13. Examining the values in line 12,
m  = 2 as expected, but the second variable, plot_seq, has two values, where only one value is expected. While plot_seq has the value expected, 2  1, it is the incorrect variable for plotting. Instead, seq_length(m) should be plotted.


  Stepping Through an M-File Correcting Problems and Ending Debugging