Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

save

Save workspace variables to file

Syntax

Description

example

save(filename) saves all variables from the current workspace in a MATLAB® formatted binary file (MAT-file) called filename. If filename exists, save overwrites the file.

example

save(filename,variables) saves only the variables or fields of a structure array specified by variables.

example

save(filename,variables,fmt) saves in the file format specified by fmt. The variables argument is optional. If you do not specify variables, the save function saves all variables in the workspace.

example

save(filename,variables,version) saves to the MAT-file version specified by version. The variables argument is optional.

example

save(filename,variables,'-append') adds new variables to an existing file. If a variable already exists in a MAT-file, then save overwrites it. The variables argument is optional.

To append to a Version 6 MAT-file, you must also include '-v6' as an input argument.

example

save filename 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 quotes. Separate inputs with spaces instead of commas.

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.

Examples

collapse all

Save all variables from the workspace in a binary MAT-file, test.mat. If filename is a variable, use function syntax.

filename = 'test.mat';
save(filename)

Otherwise, you also can use command syntax.

save test.mat

Remove the variables from the workspace, and then retrieve the data with the load function.

clear
load('test.mat')

Create and save two variables, p and q, to a file called pqfile.mat.

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

MATLAB® saves the variables to the file, pqfile.mat, in the current folder.

You also can use command syntax to save the variables, p and q.

save pqfile.mat p q

Create two variables, save them to an ASCII file, and then view the contents of the file.

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

The type function displays the contents of the file.

Alternatively, use command syntax for the save operation.

save pqfile.txt p q -ascii

Create a structure, s1, that contains three fields, a, b, and c.

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

Save the fields of structure s1 as individual variables in a file called newstruct.mat.

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

Check the contents of the file using the whos function.

disp('Contents of newstruct.mat:')
whos('-file','newstruct.mat')
Contents of newstruct.mat:
  Name      Size            Bytes  Class     Attributes

  a         1x1                 8  double              
  b         1x2               262  cell                
  c         1x6                12  char                

Create two variables and save them to a version 7.3 MAT-file called example.mat.

A = rand(5);
B = magic(10);
save('example.mat','A','B','-v7.3')

You also can use command syntax for the save operation.

save example.mat A B -v7.3

Save two variables to a MAT-file. Then, append a third variable to the same file.

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

View the contents of the MAT-file.

whos('-file','test.mat')
  Name       Size            Bytes  Class     Attributes

  p          1x10               80  double              
  q         10x10              800  double              

Create a new variable, a, and append it to the MAT-file.

a = 50;
save('test.mat','a','-append')

View the contents of the MAT-file.

whos('-file','test.mat')
  Name       Size            Bytes  Class     Attributes

  a          1x1                 8  double              
  p          1x10               80  double              
  q         10x10              800  double              

The variable, a, is appended to test.mat, without overwriting the previous variables, p and q.

    Note:   To append to a Version 6 MAT-file, specify both '-v6' and '-append'. For example, to save variable a to the file, test.mat, call:

    save('test.mat','a','-v6','-append')

Input Arguments

collapse all

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

If filename has no extension (that is, no period followed by text), and the value of format is -mat (the default), then MATLAB appends .mat. If filename does not include a full path, MATLAB saves to the current folder. You must have permission to write to the file.

When using the command form of save, it is unnecessary to enclose the input in single quotes. However, if filename contains a space, you must enclose the argument in single quotes. For example, save 'filename withspace.mat'.

Example: 'myFile.mat'

Data Types: char

Names of variables to save, specified as one or more character vectors. When using the command form of save, you do not need to enclose the input in single quotes. variables can be in one of the following forms.

Form of variables InputVariables to Save
var1,...,varNSave the listed variables, specified as individual character vectors.
Use the '*' wildcard to match patterns. For example, save('filename.mat','A*') saves all variables in the file that start with A.
'-regexp',expr1,...,exprNSave only the variables whose names match the regular expressions, specified as character vectors. For example, save('filename.mat','-regexp','^Mon','^Tues') saves only the variables in the file whose names begin with Mon or Tues.
'-struct',structName Store the fields of the scalar structure specified by structName as individual variables in the file. For example, save('filename.mat','-struct','S') saves the scalar structure, S.
'-struct',structName,field1,...,fieldNStore the specified fields of the specified scalar structure as individual variables in the file. For example, save('filename.mat,'-struct','S','a','b') saves the fields S.a and S.b.
'-struct',structName,'-regexp',expr1,...,exprNStore only the fields whose names match the regular expressions, specified as character vectors.

Data Types: char

File format, specified as one of the following. When using the command form of save, you do not need to enclose the input in single quotes, for example, save myFile.txt -ascii -tabs.

Value of fmtFile Format
'-mat'

Binary MAT-file format.

'-ascii'

Text format with 8 digits of precision.

'-ascii','-tabs'

Tab-delimited text format with 8 digits of precision.

'-ascii','-double'

Text format with 16 digits of precision.

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

Tab-delimited text format with 16 digits of precision.

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.

Use one of the text formats to save MATLAB numeric values to text files. In this case:

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

  • 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.

If you specify a text format and any variable is a two-dimensional character array, then MATLAB translates characters to their corresponding internal ASCII codes. For example, 'abc' appears in a text file as:

  9.7000000e+001  9.8000000e+001  9.9000000e+001

MAT-file version, specified as one of the following. When using the command form of save, you do not need to enclose the input in single quotes.

Value of versionLoads in MATLAB VersionsSupported FeaturesCompressionMaximum Size of Each Variable
'-v7.3'7.3 (R2006b) or later

Saving and loading parts of variables, and all Version 7 features

Yes≥ 2 GB on 64-bit computers
'-v7'7.0 (R14) or later

Unicode® character encoding, which enables file sharing between systems that use different default character encoding schemes, and all Version 6 features.

Yes2^31 bytes per variable
'-v6'5 (R8) or later

N-dimensional arrays, cell arrays, structure arrays, variable names longer than 19 characters, and all Version 4 features.

No2^31 bytes per variable
'-v4'All

Two-dimensional double, character, and sparse arrays

No100,000,000 elements per array, and 2^31 bytes per variable

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 current version of MATLAB software.

    Note:   Version 7.3 MAT-files use an HDF5 based format that requires some overhead storage to describe the contents of the file. For cell arrays, structure arrays, or other containers that can store heterogeneous data types, Version 7.3 MAT-files are sometimes larger than Version 7 MAT-files.

To view or set the default version for MAT-files, select a MAT-file save format option in the General Preferences.

More About

collapse all

Tips

  • 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.

See Also

| | | | | |

Introduced before R2006a

Was this topic helpful?