save
Save variables from workspace to file
Syntax
Description
save(
saves all variables
from the current workspace in a binary MATLAB® file (MAT-file) named filename
)filename
. If
filename
exists, save
overwrites
the file.
save(
adds new variables to an existing file. If a variable already exists in a
MAT-file, then filename
,variables
,"-append")save
overwrites it with the value in the
workspace.
For ASCII files, "-append"
adds data to the end of the
file.
To append to a Version 6 MAT-file, you must also specify
version
as "-v6"
.
save
is the command form of
the syntax. Command form requires fewer special characters. You do not need to
type parentheses or enclose the input in single or double quotes. Separate
inputs with spaces instead of commas. If any input includes spaces, enclose it
in single quotes.filename
For example, to save a file named test.mat
, these
statements are
equivalent:
save test.mat % command form save("test.mat") % function form
You can include any of the inputs described in previous syntaxes. For example,
to save the variable X
to a file named my
file.mat
:
save 'my file.mat' X % command form, using single quotes save("my file.mat","X") % function form, using double quotes
Do not use command form when any of the inputs, such as
filename
, are variables.
Examples
Input Arguments
Limitations
Attempting to save data from two separate MATLAB sessions to the same file at the same time may lead to corruption of the file.
Tips
For more flexibility in creating ASCII files, use
fprintf
.Saving graphics objects with the
save
function can result in a large file because the file contains all the information required to regenerate the object.Saving figures with the
save
function is not recommended. Use thesavefig
function instead. Usingsave
to save a figure in R2014b or later makes a MAT-file inaccessible in earlier versions of MATLAB. If you usesave
to save a figure, then the function displays a warning message. Delete any figures before usingsave
. Keep in mind that the figures might not be directly in your workspace, but might, for example, be stored in a structure or in the workspace of a callback function.The
filename
argument can be any name that is valid on the current platform. However, to ensure theload
function can access the file on any platform, do not use any of these characters infilename
:\
(backslash),/
(forward slash),:
(colon),*
(asterisk),?
(question mark),"
(double quotation mark),<
(less than sign),>
(greater than sign),|
(pipe),'
(apostrophe), or;
(semicolon).