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.
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 browser, right-click, and then select Save As. 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 Save As option, and in the Save As window, 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 that of the script.
You also can save workspace variables programmatically
save function. For example, to save all
current workspace variables to the file
june10.mat, use the
Bto the file
june10.mat, use the command
To store fields of a scalar structure as individual variables, use the
save 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( 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.
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.
To load variables saved to a MATLAB script into the workspace, simply run the script.
You also can load saved variables programmicatically, use the
load function. For example, load all variables from the file
mapfrom the file
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.
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
load function to load the variables into a structure. For
S = load('durer') loads all the variables from the file
durer.mat into the structure
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
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
whos -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, see MAT-File Versions.