Save workspace variables to file
save(
saves
all variables from the current workspace in a MATLAB® formatted
binary file (MAT-file) called 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 include '-v6'
as
an input argument.
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.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 named X
:
save test.mat X % command form save('test.mat','X') % function form
Do not use command form when any of the inputs, such as filename
, are
variables or strings.
For more flexibility in creating ASCII files, use dlmwrite
or fprintf
.
Saving graphics objects with the save
function
can result in a large file since the file contains all the information
required to regenerate the object.
Avoid saving figures with the save
function.
Use the savefig
function instead.
Using save
to save a figure in R2014b or later
makes MAT-file inaccessible in earlier versions of MATLAB. If
you use save
to save a figure, then the function
displays a warning message. Delete any figures before using save
.
Keep in mind that the figures might not be directly in your workspace.
For example, they might be stored in a structure or in the workspace
of a callback function.