SAVE Save Variables To A File

Section: Input/Ouput Functions

Usage

Saves a set of variables to a file in a machine independent format. There are two formats for the function call. The first is the explicit form, in which a list of variables are provided to write to the file:
  save filename a1 a2 ...

In the second form,

  save filename

all variables in the current context are written to the file. The format of the file is a simple binary encoding (raw) of the data with enough information to restore the variables with the load command. The endianness of the machine is encoded in the file, and the resulting file should be portable between machines of similar types (in particular, machines that support IEEE floating point representation).

You can also specify both the filename as a string, in which case you also have to specify the names of the variables to save. In particular

   save('filename','a1','a2')

will save variables a1 and a2 to the file.

Starting with version 2.0, FreeMat can also read and write MAT files (the file format used by MATLAB) thanks to substantial work by Thomas Beutlich. Support for MAT files is still in the alpha stages, so please be cautious with using it to store critical data. Also, things like objects wont be saved properly, as will variables that dont exist in MATLAB such as single-precision sparse types. The file format is triggered by the extension. To save files with a MAT format, simply use a filename with a ".mat" ending.

Example

Here is a simple example of save/load. First, we save some variables to a file.
--> D = {1,5,'hello'};
--> s = 'test string';
--> x = randn(512,1);
--> z = zeros(512);
--> who
  Variable Name      Type   Flags             Size
              A     float                    [512 512]
              D      cell                    [1 3]
            ans     int32                    [1 1]
             fp    uint32                    [1 1]
              i     int32                    [1 1]
              l      cell                    [1 5]
              n    uint32                    [1 1]
              s    string                    [1 11]
              x    double                    [512 1]
              y     float                    [1 1024]
              z     float                    [512 512]
--> save loadsave.dat

Next, we clear all of the variables, and then load them back from the file.

--> clear all
--> who
  Variable Name      Type   Flags             Size
--> load loadsave.dat
--> who
  Variable Name      Type   Flags             Size
              A     float                    [512 512]
              D      cell                    [1 3]
            ans    double                    [0 0]
             fp    uint32                    [1 1]
              i     int32                    [1 1]
              l      cell                    [1 5]
              n    uint32                    [1 1]
              s    string                    [1 11]
              x    double                    [512 1]
              y     float                    [1 1024]
              z     float                    [512 512]