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 if 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, 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. Therefore, continue or step through the first iteration of the loop in collatzplot when m = 1. When collatzplot stops at line 10 the next time (when m = 2), step in to the collatz function so you can examine values there.

Selecting the Workspace.   Variables assigned through the Command Window are considered to be 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. 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.

Viewing Datatips in the Editor/Debugger.   In the Editor/Debugger, position the cursor to the left of a variable. Its current value appears and stays in view until you move the cursor - this is called a datatip. In the example, 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.   Type a variable name in the Command Window and MATLAB displays its current value. To see the variables currently in the workspace, use who. To see the value of n for the example, 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, 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. Because next_value is 1, the while loop ends. The pause indicator is at line 10 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 in collatzplot and advance to line 11, then line 12. The variable sequence_length 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 has two values, where only one value is expected. In line 13, the second variable, plot_seq has the value expected, 2  1, but plot_seq is the incorrect variable for plotting. Instead, the variable seq_length should be plotted.


 Stepping Through an M-File Correcting Problems and Ending Debugging