Skip to content

MATLAB - Workspace

The MATLAB Workspace is your interactive environment that stores all variables you create or import during a session. Think of it as a temporary memory space for your current work. Each variable has a name, value, and other attributes like its size and data type. Mastering workspace management is crucial for efficient and reproducible analysis.

This chapter will guide you through the essentials of interacting with the workspace, covering:

  • Viewing Workspace Contents
  • Creating and Modifying Variables
  • Saving and Loading Workspace Data for Future Use
  • Clearing Variables from Memory

In the default MATLAB Desktop layout, the Workspace pane is typically located on the right. If it’s not visible, you can enable it from the main menu:

On the Home tab, in the Environment section, click Layout. In the dropdown menu, ensure that Workspace is checked. This will display the pane, which lists variables in a table format with columns for Name, Value, Class, Size, etc.

You can also inspect workspace contents programmatically using the who and whos commands in the Command Window.

% The 'who' command lists the names of variables currently in the workspace.
who
Your variables are:
A myString
% The 'whos' command provides a more detailed view.
whos
Name Size Bytes Class Attributes
A 3x3 72 double
myString 1x11 22 string

Any variable you define in the Command Window or in a script automatically appears in the Workspace. Let’s create a matrix and a string variable.

A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
myString = "Hello, MATLAB!"
myString =
"Hello, MATLAB!"

After executing these commands, you’ll see A and myString in the Workspace pane. You can double-click a variable in the pane to open it in the Variables editor, where you can inspect and manually edit its values, which is particularly useful for debugging.

To open a variable programmatically in the editor, use the openvar command:

openvar('A')

Workspace variables are cleared when you exit MATLAB. To preserve your data, you must save it to a file. MATLAB’s native format is the MAT-file (.mat), which is an efficient, compressed binary format.

The most flexible way to manage data persistence is with the save and load commands.

% Save all variables from the current workspace to a file named 'my_session.mat'
save('my_session.mat')
% Save only specific variables (e.g., A and myString) to a file
save('important_data.mat', 'A', 'myString')

To retrieve the data in a new MATLAB session, use the load command:

% Load all variables from the specified MAT-file into the workspace
load('my_session.mat')
% You can also load data into a structure, which is useful for preventing
% variable name conflicts in your current workspace.
data = load('important_data.mat')
data =
struct with fields:
A: [3×3 double]
myString: "Hello, MATLAB!"

While MAT-files are great for MATLAB, if you need to share data with other programs (like Python or Excel), consider saving variables in standard formats. Use functions like writematrix, writetable, or writecell for this purpose.

It’s good practice to clear your workspace to avoid using old variable values by mistake. The clear command is used for this.

% Remove all variables from the workspace
clear
% Create some new variables
x = 10; y = 20;
% Remove only the variable 'x' from the workspace
clear x
% Use a regular expression to clear variables starting with 'test'
test1 = 1; test_data = 2; other_var = 3;
clear -regexp ^test

A common error is running a script that relies on variables that are no longer in the workspace. To make your scripts robust and sharable, always initialize all necessary variables at the beginning of the script or load them from a data file. A clear command at the top of a main script is often a good idea to ensure a clean starting point.