askvity

How to See Variables in MATLAB?

Published in MATLAB Variables 4 mins read

You can easily see variables in MATLAB using built-in tools like the Workspace window, the Command Window, and specific app interfaces like the MATLAB® Coder™ app.

MATLAB provides several convenient ways to inspect the variables you create and use during your programming sessions. Understanding these methods is crucial for debugging, analyzing results, and monitoring your code's execution.

Essential Ways to View Variables in MATLAB

The most common methods for viewing variables are available directly within the MATLAB environment:

1. Using the Workspace Window

The Workspace window is a dynamic view of all variables currently active in your MATLAB session or within the scope of the function or script you are currently debugging.

  • What it shows: Variable names, their values (or a summary for large arrays/complex types), data types, dimensions, and memory usage.
  • How to access: It's typically displayed by default in the MATLAB desktop layout. If not visible, go to the "Layout" tab and select "Workspace".
  • Interaction: You can double-click on a variable in the Workspace to open the Variable Editor, which allows you to view and even modify the contents of arrays, tables, structures, and other data types in a spreadsheet-like format.

2. Using the Command Window

The Command Window is where you execute MATLAB commands interactively. You can also use it to display variable values.

  • How to view a variable: Simply type the name of the variable and press Enter. MATLAB will display its value in the Command Window.
    >> myVariable = 10;
    >> myVariable
    myVariable =
        10
  • Displaying multiple variables: You can display multiple variables sequentially.
  • Limitations: This is best for scalar values or small arrays. Displaying large arrays or complex structures here can clutter the window. Using a semicolon (;) after a command suppresses the output in the Command Window.

3. Using the disp Function

The disp function is another way to display the value of a variable in the Command Window, often used within scripts or functions when you want to programmatically output information.

  • Syntax: disp(variableName)
  • Example:
    >> result = [1 2 3];
    >> disp(result);
        1     2     3

4. Using the whos Function

The whos function provides a summary of the variables in the current workspace, similar to what the Workspace window shows but displayed in the Command Window.

  • What it shows: Variable name, size, bytes, class (data type), and attributes.

  • Example:

    >> whos
      Name               Size            Bytes  Class     Attributes
    
      myVariable         1x1                 8  double
      result             1x3                24  double

Viewing Variables in Specific MATLAB Apps

Certain MATLAB apps, like those for code analysis or fixed-point conversion, offer specialized ways to inspect variables relevant to the task at hand.

5. In the MATLAB® Coder™ App (Fixed-Point Conversion)

When using the MATLAB® Coder™ app to convert MATLAB code to fixed-point, you can analyze variables specifically within that context.

  • Method: On the Convert to Fixed Point page of the app, you can view information about the variables in the MATLAB functions.
  • How to see variables: To view information about the variables for the function selected in the Source Code pane, use the Variables tab or pause over a variable in the code window. This allows you to see details about the variable's proposed fixed-point type, range, and other relevant properties derived during the analysis.
Method Location Best For How to Use
Workspace Window MATLAB Desktop Overview, exploring contents Double-click variable or view table
Command Window MATLAB Desktop Quick value check (scalar/small array) Type variable name & press Enter
disp function Command Window/Code Programmatic output disp(variable)
whos function Command Window Summary of all variables Type whos & press Enter
MATLAB Coder App (FP) MATLAB Coder App Fixed-point details during conversion Use Variables tab or pause over variable

Each method serves a slightly different purpose, from getting a quick value in the Command Window to a detailed inspection in the Variable Editor or specialized analysis within an app like MATLAB Coder.

Related Articles