Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

Save and Load Workspace Variables

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 load MAT-files later during the current MATLAB session, or during another session, if you want to reuse the workspace variables.

This table describes how to save and load workspace variables.

ActionProcedure

Save all workspace variables to a MAT-file

On the Home tab, in the Variable section, click Save Workspace.

You can also use the save function. For example, save all current workspace variables to the file june10.mat

save('june10')

Save selected variables to a MAT-file

Do one of the following:

  • Select the variables in the Workspace browser, right-click, and then select Save As.

  • Drag the variables from the Workspace browser to the Current Folder browser.

You can also use the save function. For example, save only variables A and B to the file june10.mat

save('june10','A','B')

Save part of a variable

Use the matfile function. For an example, see Save Parts of Variables to MAT-Files.

Load a MAT-file

Select the MAT-file in the Current Folder browser, right-click, and then select Load.

You can also use the load function. For example, load all variables from the file durer.mat

load('durer')

Load selected variables from a MAT-file

Do one of the following:

  • On the Home tab, in the Variable section, click Import Data. Select the MAT-file you want to load and click Open.

  • In the Current Folder browser, select the MAT-file that contains the variables. Drag variables from the Details panel of the Current Folder browser to the Workspace browser.

You can also use the load function. For example, load variables X and map from the file durer.mat

load('durer','X','map')

Load part of a variable

Use the matfile function. For an example, see Load Parts of Variables from 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.

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.

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.

Save Variables to MATLAB Script

You also can save workspace variables to a MATLAB script:

  • To save all workspace variables, on the Home tab, click Save Workspace.

  • To save selected workspace variables, select the variables in the Workspace browser, right-click, and then select Save As.

Then, in the Save As window, specify a file name. In the Save as type menu, select 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.

To load the saved variables into the workspace, simply run the script.

Save Structure Fields as Separate Variables

If any of the variables in your current workspace are structure arrays, the default behavior for saving is to store the entire structure. To store fields of a scalar structure as individual variables, use the save function in the Command Window with the -struct option.

For example, consider the structure S.

S.a = 12.7;  S.b = {'abc', [4 5; 6 7]};  S.c = 'Hello!';

Save the entire structure to newstruct.mat. The file contains the variable S.

save newstruct.mat S
whos -file newstruct.mat
  Name      Size                   Bytes  Class     Attributes
  S         1x1                      810  struct

Save the fields individually. The file contains variables a, b, and c, but not S.

save newstruct.mat -struct S
whos -file newstruct.mat
  Name      Size                   Bytes  Class     Attributes
  a         1x1                        8  double
  b         1x2                      262  cell
  c         1x6                       12  char

Save only selected fields, such as a and c. The file contains variables a and c, but not b or S.

save newstruct.mat -struct S a c
whos -file newstruct.mat
  Name      Size                   Bytes  Class     Attributes
  a         1x1                        8  double
  c         1x6                       12  char

More About

Was this topic helpful?