Kahibaro
Discord Login Register

Inspecting Variable Values During Execution

Why Inspecting Variables While Code Runs Matters

When you debug MATLAB code, you often need to know what is inside your variables at specific moments of execution. Inspecting variable values during execution lets you compare what you expect to happen with what actually happens. This is one of the key ways to understand mistakes in your logic, incorrect sizes of arrays, wrong units, or unexpected data types.

Once code is paused at a breakpoint or stopped on an error, MATLAB gives you several tools to look inside your variables. In this chapter you focus on these tools and how to use them effectively while your code is running or paused.

Pausing Execution and the “K>>” Prompt

To inspect variables during execution, MATLAB must be in a paused state. This typically happens in two ways. You either hit a breakpoint that you set earlier, or MATLAB stops when it encounters a runtime error while running your script or function.

When MATLAB is paused, you will see the prompt in the Command Window change from >> to K>>. The Editor will highlight the line where execution is paused, often with a green arrow pointing at that line. At that moment, you can run commands in the Command Window that read or modify variables in the current workspace. This current workspace depends on where you are paused. If you are inside a function, you are looking at the function workspace. If you are in a script, you are looking at the base workspace for that script.

As long as MATLAB shows the K>> prompt, you can inspect variables, call functions, and even change values before continuing execution.

Viewing Variables in the Command Window

The most direct way to inspect a variable is to type its name at the K>> prompt. For example, if you are paused inside a loop and type:

matlab
x

MATLAB prints the current value of x in the Command Window. This can be a scalar, vector, matrix, table, or any other variable type. If the variable is very large, you can examine only part of it by indexing:

matlab
x(1:10)
x(1:5, 1:5)

You can also use inspection functions such as size, class, and whos to get quick information about shape and type. For instance,

matlab
size(A)
class(A)
whos A

can confirm that your array A has the dimensions and type you expect.

This type of quick inspection is especially useful inside loops or conditional blocks, where the values can change from one iteration or branch to another.

Using the Workspace Browser While Paused

When MATLAB is paused, the Workspace browser updates to show the variables that are visible in the current context. If you paused inside a function, the Workspace browser will typically show only that function’s variables. If you step into another function, the view can change as MATLAB switches context.

You can sort the Workspace columns by name, value, class, or size to find the variable you are interested in. When you double click a variable in the Workspace while paused, MATLAB opens an appropriate viewer. For numeric arrays, this launches the Variable Editor, which you can use to see values in a grid.

If you are running nested function calls and you use stepping operations, the values a variable holds can change between steps. The Workspace browser reflects these changes as you go, so you can watch variables evolve while you debug.

Inspecting Values with the Variable Editor

The Variable Editor is a spreadsheet-like window that opens when you double click a variable in the Workspace browser or when you use functions such as openvar or open. While code is paused, the Variable Editor shows the current values. If you modify entries directly in the Variable Editor, you are modifying the actual variables in the paused workspace.

This can be especially useful when you want to test what happens if a variable takes a different value without restarting your script. For example, you can pause at a breakpoint, change a single element of an array from 0 to 1, and then continue execution to observe the effect.

For large variables, the Variable Editor allows you to scroll, sort columns for tables, and adjust display options. However, editing large arrays can be slow, so in that case it is often better to change values through small commands in the Command Window.

Inspecting Values with the Data Tips in the Editor

When MATLAB is paused at a breakpoint, you can move your mouse cursor over a variable name in the Editor to see its value in a small popup that appears next to the code. This is called a data tip. For example, if your code has the line

matlab
y = a(i) + b(i);

and MATLAB is paused at that line, you can hover over a, b, i, or y to see their current values. For scalars, MATLAB shows the value directly. For arrays, you often see the size and class, and you may see a small preview of some elements.

This method is useful for quickly confirming that a loop index or a condition variable has the correct value without leaving the Editor. It also reduces the need to switch to the Command Window for simple checks.

If you want to explore further, you can click in the data tip and select options to open the variable in the Variable Editor or to copy its name or value.

Evaluating Selected Code While Paused

While your program is paused, you can evaluate parts of a line or any selected code in the Editor. This is useful to inspect intermediate results that are not stored in a variable yet, or to test expressions using current variable values.

For example, suppose your line of code is

matlab
z = sin(x.^2 + y.^2);

and MATLAB is paused at this line. You can select the expression x.^2 + y.^2, right click, and choose to evaluate the selection, or you can use the corresponding keyboard shortcut if available. MATLAB then runs that expression using the current values of x and y and prints the result in the Command Window or shows it in a tooltip, depending on your version.

You can also type arbitrary expressions at the K>> prompt, such as

matlab
max(x)
mean(y)
x(1:3) + y(1:3)

to probe the current state of your data. These commands are executed in the same paused workspace, so they can access all variables that are in scope at the breakpoint.

Inspecting Variables in Different Call Stack Levels

When debugging functions that call other functions, MATLAB maintains a call stack. The call stack is the list of functions that have been called and not yet returned. While paused in a deeper function, you might want to inspect variables from a higher level function in the stack.

In the Editor, the call stack is usually shown in a panel or in the “Current Folder” / “Function Call Stack” view. Each entry corresponds to a function frame. You can click different frames in the call stack to switch the debugging context. When you select a different frame, the Workspace browser updates to show the variables that belong to that function, and the current line indicator moves to the corresponding location in that file.

This switching lets you inspect variables at multiple levels of your program. For instance, you can pause inside a helper function but still look at the input arguments as they appear in the caller function, then return to the helper function frame to continue stepping.

This is particularly helpful when you pass large structures or arrays between functions and you suspect that something goes wrong at a certain point in the call chain. By selecting each frame, you see how the values change as they move through your program.

Temporarily Modifying Variables While Debugging

When code is paused, you can change variable values to test what would happen under different conditions, without modifying the source file. You can do this by assigning new values at the K>> prompt:

matlab
threshold = 0.5;
A(1,1) = 10;
flag = true;

or by editing directly in the Variable Editor. After you modify values, you can step to the next line or continue execution. The code will then use the updated values.

This technique is helpful for exploring edge cases or confirming that a suspected bug is related to a specific value. For example, if your code behaves incorrectly only for n = 0, you can pause before the error, change n to 0, and then step through to see exactly how the logic fails.

Remember that any changes you make while paused are not automatically written back into your script or function files. They only affect the current debugging session. Once you stop debugging and rerun the code, variables are created again according to your source code.

Watching Variables Across Steps

A key part of debugging is watching how a variable changes from one line to the next. After you hit a breakpoint, you can use step commands to move through code line by line. At each step, you can use any of the inspection methods described so far.

For instance, you can pause before a loop, confirm the initial value of a counter or accumulator, then step into the loop and inspect that variable after each iteration. By looking at the values line by line, you can often see exactly where they start to diverge from what you expect.

If a variable suddenly becomes NaN, Inf, or an unexpected size, the step where that happens is often the line you need to fix. Visual inspection while stepping is especially effective for catching indexing mistakes and division by zero.

Inspecting Values on Errors

When MATLAB hits a runtime error, it stops execution and shows the error message in red text. In many cases, MATLAB offers a link in the error message that says something like “Open in Editor” or “View in Editor.” If you click this link, MATLAB opens the file at the line that caused the error.

You can also enable automatic pause on errors with settings such as “Pause on Errors” from the Editor or via commands that control debugging behavior. When this is turned on, MATLAB enters the paused state at the error line, giving you the K>> prompt. You can then inspect all available variables exactly as they were at the time of the error.

By examining these variables, you can often see the cause of the error. For example, if the error is “Index exceeds the number of array elements,” you can check the value of the index and the size of the array at the error line to understand the mismatch.

Using Display Functions for Structured Variables

Some variables, such as structures, tables, and objects, can be hard to understand if you just print them at the K>> prompt. For these, specialized display functions can help.

For example, if you have a structure s, you can type:

matlab
fieldnames(s)

to see its fields, then inspect individual fields like s.data or s.info.name. For tables, you might print only the first few rows:

matlab
head(T)

to get a feel for the variables inside without printing the whole dataset. While paused, these commands help you navigate complex data more quickly.

The same idea applies to objects from toolboxes. Often there are methods like summary, plot, or disp that give a more human friendly view. You can call these methods during debugging to understand the state of the object at the time of the pause.

Restoring Normal Execution After Inspection

After you finish inspecting variables, you typically resume execution with one of the continue commands. Using the Editor toolbar or keyboard shortcuts, you can choose to continue to the next breakpoint, step to the next line, or step out of the current function. Once MATLAB leaves the paused state, the K>> prompt returns to >> and you can no longer access the paused workspace.

If you need to debug again, you must rerun your script or function. Any variable changes you made only live in the paused context and will be recreated from scratch the next time you run your code.

Being aware of this separation helps avoid confusion. If you relied on experimental changes you made while paused, remember to apply the necessary changes in your source files, not just in the debugging workspace.

Key points to remember:

  1. You can inspect variables only while MATLAB is paused, for example at a breakpoint or on an error, indicated by the K>> prompt.
  2. Use the Command Window, Workspace browser, Variable Editor, and data tips in the Editor to view and modify variable values.
  3. You can switch call stack frames to inspect variables in different functions, and any changes you make while paused affect only the current debugging session, not your source code.

Views: 4

Comments

Please login to add a comment.

Don't have an account? Register now!