MATLAB Function Reference    
save

Save workspace variables on disk

Graphical Interface

As an alternative to the save function, select Save Workspace As from the File menu in the MATLAB desktop the Workspace browser.

Syntax

Description

save by itself, stores all workspace variables in a binary format in the current directory in a file named matlab.mat. Retrieve the data with load. MAT-files are double-precision, binary, MATLAB format files. They can be created on one machine and later read by MATLAB on another machine with a different floating-point format, retaining as much accuracy and range as the different formats allow. They can also be manipulated by other programs external to MATLAB.

save filename stores all workspace variables in the current directory in filename.mat. To save to another directory, use the full pathname for the filename.

save filename var1 var2 ... saves only the specified workspace variables in filename.mat. Use the * wildcard to save only those variables that match the specified pattern. For example, save('A*') saves all variables that start with A.

save ... option saves the workspace variables in the format specified by option

option Argument
Result: How Data is Stored
-append
The specified existed MAT-file, appended to the end
-ascii
8-digit ASCII format
-ascii -double
16-digit ASCII format
-ascii -tabs
delimits with tabs
-ascii -double -tabs
16-digit ASCII format, tab delimited
-mat
Binary MAT-file form (default)
-v4
A format that MATLAB version 4 can open

Variables saved in ASCII format merge into a single variable that takes the name of the ASCII file. Therefore, save only one variable at a time. If you save more than one variable using an ASCII format, loading filename results in a single workspace variable named filename; use the colon operator to access individual variables.

With the v4 flag, you can only save data constructs that are compatible with versions of MATLAB 4. Therefore, you cannot save structures, cell arrays, multidimensional arrays, or objects. In addition, you must use filenames that are supported by MATLAB version 4.

Saving complex data with the -ascii keyword causes the imaginary part of the data to be lost, as MATLAB cannot load nonnumeric data ('i').

save('filename', ...) is the function form of the syntax.

For more control over the format of the file, MATLAB provides other functions, as listed in See Also, below.

Algorithm

The binary formats used by save depend on the size and type of each array. Arrays with any noninteger entries and arrays with 10,000 or fewer elements are saved in floating-point formats requiring 8 bytes per real element. Arrays with all integer entries and more than 10,000 elements are saved in the formats shown, requiring fewer bytes per element.

Element Range
Bytes per Element
0 to 255
1
0 to 65535
2
-32767 to 32767
2
-231+1 to 231-1
4
other
8

External Interfaces to MATLAB provides details on reading and writing MAT-files from external C or Fortran programs. It is important to use recommended access methods, rather than rely upon the specific MAT-file format, which is likely to change in the future.

Examples

To save all variables from the workspace in binary MAT-file, test.mat, type

To save variables p and q in binary MAT-file, test.mat, type

To save the variables vol and temp in ASCII format to a file named june10, type

See Also
diary, fprintf, fwrite, load, workspace


 rsf2csf saveas