save

Save variables in a the .MAT format file

Syntax

save(filename)

save(filename, format)

save(filename, variables)

save(filename, variables, format)

save(filename, '-struct', structvariables, format)

save(filename, ..., options)

Inputs

filename
Name of the file to save in .MAT format. By default, the file saved will be saved in -v5 binary format and overwritten, unless specified by format or options.
Type: string
format
Optional argument specifying the MAT format of filename.
-5
-v5
filename is in v5 format. This is the default format that filename is saved in.
Type: string
-7.3
-v7.3
filename is in v7.3 format.
Type: string
options
-append
filename will not be overwritten and the variables to be saved will be appended to the existing file, it is exists.
Type: string
-ascii
filename will be saved as a text file and real, numerical data will be written.
Type: string
variable
Optional list of the variables to save, separated by a comma.
If no variable is specified, all variables are saved. Function handles cannot be saved and will appear as 'NaN' in filename. The single command can be used to mark a scalar/complex/2DMatrix variable as a single precision datatype so that single precision data can be written out in filename. If variable is a graphics or computer vision handle, it will be saved as a double in filename.
Type: string
structvariables
Optional comma separated list of the struct variables.
When used with the '-struct' keyword, the fields in the specified structs will be saved as variables in filename.
Type: string

Examples

Save data to a .mat file using save function:
st='hello';
M = [1923.71288  4023.03575  9768.82832  9195.83701  104.13143  4261.35201];

save('file.mat')             % Saves all varaibles
save('file.mat','M')         % Save specific variable
save('file.mat','M','-v7.3') % Save specific variable with v7.3 format
Save a 2D matrix with single precision data in a .mat file:
M = [1923.71288  4023.03575  9768.82832  9195.83701  104.13143  4261.35201];
single(M);
save('file.mat','M');
Append variables to an existing file:
M = [1923.71288  4023.03575  9768.82832  9195.83701  104.13143  4261.35201];
save('file.mat');              % Overwrite, if file exists
a = 10;
save('file.mat', '-append');   % Append to previously existing file
Saving struct fields as variables:
mystruct = struct('data1',[],'data2',[], 'data3',[],'data4',[]);
mystruct.data1 = {'foo®'};
mystruct.data2 = 5;
mystruct.data3 = 4;
mystruct.data4 = 9;

curves = struct('name',[]);
curves.name = '123®';

save('struct1.mat', '-struct', 'mystruct', 'curves')
clear all
load('Session.mat')
whos

 Attr Name          Size Bytes Class 
 ==== ====          ==== ===== ===== 
      CurveFitting  1x1      8 double
      Extrapolation 1x1      8 double
      FinalCurves   1x1      8 double
      ImportData    1x1     18 cell  
      name          1x18    18 char