Save and Load Workspace Variables
Variables in the workspace do not persist across sessions of MATLAB®. When you quit MATLAB, the workspace clears. However, you can save any or all the variables in the current workspace to a MAT file. You can then reuse the workspace variables later during the current MATLAB session or during another session by loading the saved MAT file.
Save Workspace Variables
There are several ways to save workspace variables interactively:
To save all workspace variables to a MAT file, on the Home tab, in the Variable section, click Save Workspace.
To save a subset of your workspace variables to a MAT file, select the variables in the Workspace panel, right-click the selection, and then select Save Selection.
To save variables to a MATLAB script, on the Home tab, in the Variable section, click Save Workspace. Then, in the Save Workspace Variables dialog box, set the Save as type option to MATLAB Script. Variables that cannot be saved to a script are saved to a MAT file with the same name as the script.
You also can save workspace variables programmatically using the save
function. For example, save
all current workspace variables to the file
june10.mat
.
save("june10")
A
and B
to the file
june10.mat
.save("june10","A","B")
To store fields of a scalar structure as individual variables, use the
save
function with the -struct
option.
This option can be useful if you previously loaded variables from a MAT file into a
structure using the syntax S =
load(
and want to keep the
original variable structure when saving to a new MAT file.filename
)
To save part of a variable, use the matfile
function. This
functionality can be useful if you are working with very large data sets that are
otherwise too large to fit in memory. For more information, see Save and Load Parts of Variables in MAT-Files.
Note
In MATLAB Online™, variables persist between sessions.
Load Workspace Variables
To load saved variables from a MAT file into your workspace, double-click the MAT file in the Files panel. You can also drag a MAT file from the Files panel to the Workspace panel.
To load a subset of variables from a MAT file, on the Home tab, in the Variable section, click Import Data. Select the MAT file you want to load and click Open. Then, use the Import Tool dialog box to select the variables to import.
To load variables saved to a MATLAB script into the workspace, run the script.
You also can load saved variables programmatically by using the load
function. For example, load
all the variables from the file
durer.mat
.
load("durer")
X
and map
from the file
durer.mat
.load("durer","X","map")
To load part of a variable, use the matfile
function. This
function is useful if you are working with very large data sets that are otherwise
too large to fit in memory. For more information, see Save and Load Parts of Variables in MAT-Files.
Caution
When you load data into the MATLAB workspace, the new variables you create overwrite any existing
variables in the workspace that have the same name. To avoid overwriting
existing variables, load the variables into a new structure by specifying an
output argument for the load
function. For example,
S = load("durer")
loads all the variables from the
file durer.mat
into the structure
S
.
View Contents of MAT File
To see the variables in a MAT file before loading the file into your workspace, click the Preview button to the right of the file in the Files panel. The preview shows the name and value of variables in the file, as well as the file size and the date it was last modified.
You can also view the contents of a MAT file programmatically by using the command
whos
-file
filename
. This command returns the name, size, byte
count, and class of all the variables in the specified MAT file. For example, view
the contents of the example file durer.mat
.
whos -file durer.mat
Name Size Bytes Class Attributes X 648x509 2638656 double caption 2x28 112 char map 128x3 3072 double
Note
The byte counts represent the number of bytes that the data occupies in memory when loaded into the MATLAB workspace. Because of compression, data encoding, and metadata, the space occupied in the file by a variable might be different from the in-memory size. MATLAB compresses data by default in Version 7 or higher MAT files. For more information, see MAT-File Versions.