Save and Load Workspace Variables - MATLAB & Simulink (original) (raw)
The workspace is not maintained 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 (.mat
). 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 theHome 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 browser, right-click, and then select . You also can drag the selected variables from the Workspace browser to the Current Folder browser.
- To save variables to a MATLAB script, click the Save Workspace button or select the option, and in the Save As window, set the Save as type option to . Variables that cannot be saved to a script are saved to a MAT-file with the same name as that of the script.
You also can save workspace variables programmatically using the save function. For example, to save all current workspace variables to the file june10.mat
, use the command
To save only variables A
and B
to the filejune10.mat
, use the command
To store fields of a scalar structure as individual variables, use thesave
function with the -struct
option. This 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 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.
In MATLAB Onlineā¢, variables persist between sessions. Saving allows you to clear the workspace and load variables at a later time. To save variables, use thesave
or matfile
functions.
Load Workspace Variables
To load saved variables from a MAT-file into your workspace, double-click the MAT-file in the Current Folder browser.
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. You also can drag the desired variables from the Current Folder browser Details panel of the selected MAT-file to the Workspace browser. In MATLAB Online, you also can click the Preview button to the right of the MAT-file in the Files browser and drag the desired variables from the preview to the Workspace panel.
To load variables saved to a MATLAB script into the workspace, simply run the script.
You also can load saved variables programmatically, use the load function. For example, load all variables from the filedurer.mat
To load variables X
and map
from the filedurer.mat
To load part of a variable, use the matfile
function. This 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, use the load
function to load the variables into a structure. For example, S = load('durer')
loads all the variables from the filedurer.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 file name in the Current Folder browser. Information about the variables appears in the Details pane.
Alternatively, use the command whos -file
filename
. This function returns the name, dimensions, size, and class of all variables in the specified MAT-file. For example, you can 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
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 may be different from the in-memory size. MATLAB compresses data in Version 7 or higher MAT-files. For more information, seeMAT-File Versions.