| Contents | Index |
save(filename)
save(filename, variables)
save(filename, '-struct', structName, fieldNames)
save(filename, ..., '-append')
save(filename, ..., format)
save(filename, ..., version)
save filename ...
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.
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:
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):
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:
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.
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. |
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.txtAlternatively, 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');To save data from the MATLAB desktop, select File > Save Workspace As, or use the Workspace browser.
clear | hgsave | load | matfile | regexp | saveas | whos | workspace

Explore how to use MATLAB to make advancements in engineering and science.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |