Skip to Main Content Skip to Search
Product Documentation

save - Save workspace variables to file

Syntax

save(filename)
save(filename, variables)
save(filename, '-struct', structName, fieldNames)
save(filename, ..., '-append')
save(filename, ..., format)
save(filename, ..., version)
save filename ...

Description

save(filename) stores all variables from the current workspace in a MATLAB formatted binary file (MAT-file) called filename.

save(filename, variables) stores only the specified variables.

save(filename, '-struct', structName, fieldNames) stores the fields of the specified scalar structure as individual variables in the file. If you include the optional fieldNames, the save function stores only the specified fields of the structure. You cannot specify variables and the '-struct' keyword in the same call to save.

save(filename, ..., '-append') adds new variables to an existing file. You can specify the '-append' option with additional inputs such as variables, '-struct' , format, or version.

save(filename, ..., format) saves in the specified format: '-mat' or '-ascii'. You can specify the format option with additional inputs such as variables, '-struct' , '-append', or version.

save(filename, ..., version) saves to MAT-files in the specified version: '-v4', '-v6', '-v7', or '-v7.3'. You can specify the version option with additional inputs such as variables, '-struct' , '-append', or format.

save filename ... is the command form of the syntax, for convenient saving from the command line. With command syntax, you do not need to enclose input strings in single quotation marks. Separate inputs with spaces instead of commas. Do not use command syntax if inputs such as filename are variables. For more information, see Command vs. Function Syntax in the MATLAB Programming Fundamentals documentation.

Input Arguments

filename

Name of a file. If you do not specify filename, the save function saves to a file named matlab.mat.

If filename does not include an extension and the value of format is -mat (the default), MATLAB appends .mat. If filename does not include a full path, MATLAB saves in the current folder. You must have permission to write to the file.

Default: 'matlab.mat'

variables

Description of the variables to save. Use one of the following forms:

var1, var2, ...Save the listed variables.
Use the '*' wildcard to match patterns. For example, save('A*') saves all variables that start with A.
'-regexp', expressionsSave only the variables that match the specified regular expressions.
MATLAB treats all inputs as regular expressions, except the optional filename. The filename must appear immediately after the save command.

Default: all variables

'-struct'

Keyword to request saving the fields of a scalar structure as individual variables in the file. The structName input must appear immediately after the -struct keyword.

structName

Name of a scalar structure. Required when you use the '-struct' keyword.

fieldNames

Description of the fields of a structure to save as individual variables in the file. Use the same forms listed for variables. If you use the '-regexp' keyword, MATLAB treats all inputs as regular expressions except filename and structName.

'-append'

Keyword to add data to an existing file. For MAT-files, -append adds new variables to the file or replaces the saved values of existing variables with values in the workspace. For ASCII files, -append adds data to the end of the file.

format

Specifies the format of the file, regardless of any specified extension. Use one of the following combinations (not case sensitive):

'-mat'

Binary MAT-file format (default).

'-ascii'

8-digit ASCII format.

'-ascii', '-tabs'

Tab-delimited 8-digit ASCII format.

'-ascii', '-double'

16-digit ASCII format.

'-ascii', '-double', '-tabs'

Tab-delimited 16-digit ASCII format.

For MAT-files, data saved on one machine and loaded on another machine retains as much accuracy and range as the different machine floating-point formats allow.

For ASCII file formats, the save function has the following limitations:

  • Each variable must be a two-dimensional double or character array.

  • MATLAB translates characters to their corresponding internal ASCII codes. For example, 'abc' appears in an ASCII file as:

      9.7000000e+001  9.8000000e+001  9.9000000e+001
  • The output includes only the real component of complex numbers.

  • MATLAB writes data from each variable sequentially to the file. If you plan to use the load function to read the file, all variables must have the same number of columns. The load function creates a single variable from the file.

For more flexibility in creating ASCII files, use dlmwrite or fprintf.

version

Specifies the version of the file. Applies to MAT-files only.

The following table shows the available MAT-file version options and the corresponding supported features.

OptionCan Load in VersionsSupported Features
'-v7.3'7.3 (R2006b) or laterVersion 7.0 features plus support for data items greater than or equal to 2 GB on 64-bit systems.
'-v7'7.0 (R14) or laterVersion 6 features plus data compression and Unicode character encoding. Unicode encoding enables file sharing between systems that use different default character encoding schemes.
'-v6'5 (R8) or laterVersion 4 features plus N-dimensional arrays, cell arrays and structures, and variable names greater than 19 characters.
'-v4'allTwo-dimensional double, character, and sparse arrays.

If any data items require features that the specified version does not support, MATLAB does not save those items and issues a warning. You cannot specify a version later than your version of MATLAB software.

To view or set the default version for MAT-files, select File > Preferences > General > MAT-Files.

Examples

Save all variables from the workspace in binary MAT-file test.mat. Remove the variables from the workspace, and retrieve the data with the load function.

save test.mat
clear
load test.mat
 

Create a variable savefile that stores the name of a file, pqfile.mat. Save two variables to the file.

savefile = 'pqfile.mat';
p = rand(1, 10);
q = ones(10);
save(savefile, 'p', 'q')
 

Save data to an ASCII file, and view the contents of the file with the type function:

p = rand(1, 10);
q = ones(10);
save('pqfile.txt', 'p', 'q', '-ASCII')
type pqfile.txt

Alternatively, use command syntax for the save operation:

save pqfile.txt p q -ASCII
 

Save the fields of structure s1 as individual variables. Check the contents of the file with the whos function. Clear the workspace and load the contents of a single field.

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

save('newstruct.mat', '-struct', 's1');

disp('Contents of newstruct.mat:')
whos('-file', 'newstruct.mat')

clear('s1')
load('newstruct.mat', 'b')
 

Save any variables in the workspace with names that begin with Mon, Tue, or Wed to mydata.mat:

save('mydata', '-regexp', '^Mon|^Tue|^Wed');

Alternatives

To save data from the MATLAB desktop, select File > Save Workspace As, or use the Workspace browser.

See Also

clear | hgsave | load | matfile | regexp | saveas | whos | workspace

How To

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS