MATLAB Function Reference
  Go to function:
    Search    Help Desk 
save    See Also

Save workspace variables on disk

Syntax

Description

save stores all workspace variables in a binary format in the file named matlab.mat. The data can be retrieved with load.

save filename stores all workspace variables in filename.mat instead of the default matlab.mat. If filename is the special string stdio, the save command sends the data as standard output.

save filename variables saves only the workspace variables you list after the filename. For example, save myfile x y z saves only the variables x, y, and z to myfile.mat.

The function form of the syntax, save('filename'), is also permitted. So, for example, to save variables x and y to the filename myfile, use

These forms of the save command use options:

save filename options

save filename variables options

Valid option combinations are shown in the table below.

With these options:
Data is:
-ascii
stored in 8-digit ASCII format
-ascii -double
stored in 16-digit ASCII format
-ascii -tabs
stored in 8-digit ASCII format, tab-separated
-ascii -double -tabs
stored in 16-digit ASCII format, tab-separated
-V4
stored in a format that MATLAB version 4 can load
-append
added to an existing specified MAT-file

Limitations

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

Remarks

The save and load commands retrieve and store MATLAB variables on disk. They can also import and export numeric matrices as ASCII data files.

MAT-files are double-precision binary MATLAB format files created by the save command and readable by the load command. 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 disparate formats allow. They can also be manipulated by other programs, external to MATLAB.

Notes on Options

Variables saved in ASCII format merge into a single variable that takes the name of the ASCII file. Therefore, loading the file filename shown above results in a single workspace variable named filename. Use the colon operator to access individual variables.

If you save MATLAB version 5 data with the -V4 option, you must use a filename that MATLAB version 4 supports. In addition, you can only save data constructs that are compatible with MATLAB version 4; therefore, you cannot save structures, cell arrays, multidimensional arrays, or objects.

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 eight 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

The Application Program Interface Libraries contain C and Fortran routines to read and write MAT-files from external programs. It is important to use recommended access methods, rather than rely upon the specific file format, which is likely to change in the future.

See Also

fprintf, fwrite, load, quit



[ Previous | Help Desk | Next ]