Save and Load Workspace Variables - MATLAB & Simulink (original) (raw)

Main Content

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:

You also can save workspace variables programmatically using the save function. For example, save all current workspace variables to the filejune10.mat.

Alternatively, save only variables A and B to the filejune10.mat.

To store fields of a scalar structure as individual variables, use thesave 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(_`filename`_) and want to keep the original variable structure when saving to a new MAT file.

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 filedurer.mat.

Load just the variables X and map from the filedurer.mat.

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 structureS.

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 commandwhos -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.

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.

See Also

save | load

Topics