| MATLAB® | ![]() |
As an alternative to the save function, select Save Workspace As from the File menu in the MATLAB desktop, or use the Workspace browser.
save
save filename
save filename content
save filename options
save filename content options
save('filename', 'var1', 'var2', ...)
save stores all variables from the current MATLAB workspace in a MATLAB formatted file (MAT-file) named matlab.mat that resides in the current working directory. Use the load function to retrieve data stored in MAT-files. By default, MAT-files are double-precision, binary files. You can create a MAT-file on one machine and then load it on another machine using a different floating-point format, and retaining as much accuracy and range as the different formats allow. MAT-files can also be manipulated by other programs external to MATLAB.
save filename stores all variables in the current workspace in the file filename. If you do not specify an extension to the filename, MATLAB uses .mat. The file must be writable. To save to another directory, use a full pathname for the filename.
save filename content stores only those variables specified by content in file filename. If filename is not specified, MATLAB stores the data in a file called matlab.mat. See the following table.
| Values for content | Description |
|---|---|
| varlist | Save only those variables that are in varlist. You can use the * wildcard to save only those variables that match the specified pattern. For example, save('A*') saves all variables that start with A. |
| -regexp exprlist | Save those variables that match any of the regular expressions in exprlist. See the Remarks section below. |
| -struct s | Save as individual variables all fields of the scalar structure s. |
| -struct s fieldlist | Save as individual variables only the specified fields of structure s. |
In this table, the terms varlist, exprlist, and fieldlist refer to one or more variable names, regular expressions, or structure field names separated by either spaces or commas, depending on whether you are using the MATLAB command or function format. See the examples below:
Command format:
save myfile.mat firstname lastname
Function format:
save('myfile.mat', 'firstname', 'lastname')
save filename options stores all variables from the MATLAB workspace in file filename according to one or more of the following options. If filename is not specified, MATLAB stores the data in a file called matlab.mat.
| Values for options | Description |
|---|---|
| -append | Add new variables to those already stored in an existing MAT-file. |
| -format | Save using the specified binary or ASCII format. See the section on, MAT-File Format Options, below. |
| -version | Save in a format that can be loaded into an earlier version of MATLAB. See the section on Version Compatibility Options, below. |
save filename content options stores only those variables specified by content in file filename, also applying the specified options. If filename is not specified, MATLAB stores the data in a file called matlab.mat.
save('filename', 'var1', 'var2', ...) is the function form of the syntax.
The following table lists the valid MAT-file format options.
MAT-file format Options | How Data Is Stored |
|---|---|
| -ascii | Save data in 8-digit ASCII format. |
| -ascii -tabs | Save data in 8-digit ASCII format delimited with tabs. |
| -ascii -double | Save data in 16-digit ASCII format. |
| -ascii -double -tabs | Save data in 16-digit ASCII format delimited with tabs. |
| -mat | Binary MAT-file form (default). |
The following table lists version compatibility options. These options enable you to save your workspace data to a MAT-file that can then be loaded into an earlier version of MATLAB software. The resulting MAT-file supports only those data items and features that were available in this earlier version of MATLAB. (See the second table below for what is supported in each version.)
version Option | Use When Running ... | To Save a MAT-File That You Can Load In ... |
|---|---|---|
| -v7.3 | Version 7.3 or later | Version 7.3 or later |
| -v7 | Version 7.3 or later | Versions 7.0 through 7.2 (or later) |
| -v6 | Version 7 or later | Versions 5 and 6 (or later) |
| -v4 | Version 5 or later | Versions 1 through 4 (or later) |
The default version option is the value specified in the Preferences dialog box. Select File > Preferences in the Command Window, click General, and then MAT-Files to view or change the default.
The next table shows what data items and features are supported in different versions of MATLAB. You can use this information to determine which of the version compatibility options shown above to use.
| MATLAB Versions | Data Items or Features Supported |
|---|---|
| 4 and earlier | Support for 2D double, character, and sparse |
| 5 and 6 | Version 4 capability plus support for ND arrays, structs, and cells |
| 7.0 through 7.2 | Version 6 capability plus support for data compression and Unicode character encoding |
| 7.3 and later | Version 7.2 capability plus support for data items greater than or equal to 2GB |
When using the -regexp switch, save considers all variables in the argument list, with the exception of the optional filename and structure name variables, to be regular expressions. The filename, if specified, is always the first argument in the argument list, provided that this argument is a variable name. The structure name, if specified, is always the first argument following the -struct keyword, provided that the argument list includes that keyword.
When working on 64-bit platforms, you can have data items in your workspace that occupy more than 2 GB. To save data of this size, you must use the HDF5-based version of the MATLAB MAT-file. Use the –v7.3 option to do this:
save -v7.3 myfile v1 v2
If you are running MATLAB on a 64-bit computer system and you attempt to save a variable that is too large for a version 7 (or earlier) MAT-file, that is, you save without using the -v7.3 option, MATLAB skips that variable during the save operation and issues a warning message to that effect.
If you are running MATLAB on a 32-bit computer system and attempt to load a variable from a -v7.3 MAT-file that is too large to fit in 32–bit address space, MATLAB skips that variable and issues a warning message to that effect.
MAT-files saved with compression and Unicode encoding cannot be loaded into versions of MATLAB prior to MATLAB Version 7.0. If you save data to a MAT-file that you intend to load using MATLAB Version 6 or earlier, you must specify the -v6 option when saving. This disables compression and Unicode encoding for that particular save operation.
If you want to save to a file that you can then load into a Version 4 MATLAB session, you must use the -v4 option when saving. When you use this option, variables that are incompatible with MATLAB Version 4 are not saved to the MAT-file. For example, ND arrays, structs, cells, etc. cannot be saved to a MATLAB Version 4 MAT-file. Also, variables with names that are longer than 19 characters cannot be saved to a MATLAB Version 4 MAT-file.
For information on any of the following topics related to saving to MAT-files, see Exporting Data to MAT-Files in the MATLAB Programming Fundamentals documentation:
Appending variables to an existing MAT-file
Compressing data in the MAT-file
Saving in ASCII format
Saving in MATLAB Version 4 format
Saving with Unicode character encoding
Data storage requirements
Saving from external programs
For information on saving figures, see the documentation for hgsave and saveas. For information on exporting figures to other graphics formats, see the documentation for print.
Save all variables from the workspace in binary MAT-file test.mat:
save test.mat
Save variables p and q in binary MAT-file test.mat.
In this example, the file name is stored in a variable, savefile. You must call save using the function syntax of the command if you intend to reference the file name through a variable.
savefile = 'test.mat'; p = rand(1, 10); q = ones(10); save(savefile, 'p', 'q')
Save the values of variables vol and temp in ASCII format to a file named june10:
save('d:\mymfiles\june10','vol','temp','-ASCII')Save the fields of structure s1 as individual variables rather than as an entire structure.
s1.a = 12.7; s1.b = {'abc', [4 5; 6 7]}; s1.c = 'Hello!';
save newstruct.mat -struct s1;
clearCheck what was saved to newstruct.mat:
whos -file newstruct.mat Name Size Bytes Class a 1x1 8 double array b 1x2 158 cell array c 1x6 12 char array Grand total is 16 elements using 178 bytes
Read only the b field into the MATLAB workspace.
str = load('newstruct.mat', 'b')
str =
b: {'abc' [2x2 double]}Using regular expressions, save in MAT-file mydata.mat those variables with names that begin with Mon, Tue, or Wed:
save('mydata', '-regexp', '^Mon|^Tue|^Wed');Here is another way of doing the same thing. In this case, there are three separate expression arguments:
save('mydata', '-regexp', '^Mon', '^Tue', '^Wed');Save a 3000-by-3000 matrix uncompressed to file c1.mat, and compressed to file c2.mat. The compressed file uses about one quarter the disk space required to store the uncompressed data:
x = ones(3000);
y = uint32(rand(3000) * 100);
save -v6 c1 x y % Save without compression
save -v7 c2 x y % Save with compression
d1 = dir('c1.mat');
d2 = dir('c2.mat');
d1.bytes
ans =
45000240 % Size of the uncompressed data in bytes.
d2.bytes
ans =
11985283 % Size of the compressed data in bytes.
d2.bytes/d1.bytes
ans =
0.2663 % Ratio of compressed to uncompressedload, clear, diary, fileformats, fprintf, fwrite, genvarname, who, whos, workspace, regexp
![]() | run | save (COM) | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |