\section{SAVE Save Variables To A File}

\subsection{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:
\begin{verbatim}
  save filename a1 a2 ...
\end{verbatim}
In the second form,
\begin{verbatim}
  save filename
\end{verbatim}
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 \verb|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
\begin{verbatim}
   save('filename','a1','a2')
\end{verbatim}
will save variables \verb|a1| and \verb|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 in version 2.1
has improved over previous versions.  In particular, classes
should be saved properly, as well as a broader range of sparse
matrices.  Compression is supported for both reading and writing
to MAT files.  MAT file support is still in the alpha stages, so 
please be cautious with using it to store critical 
data.  The file format is triggered
by the extension.  To save files with a MAT format, simply
use a filename with a ".mat" ending.

The \verb|save| function also supports ASCII output.  This is a very limited
form of the save command - it can only save numeric arrays that are
2-dimensional.  This form of the \verb|save| command is triggered using
\begin{verbatim}
   save -ascii filename var1 var 2
\end{verbatim}
although where \verb|-ascii| appears on the command line is arbitrary (provided
it comes after the \verb|save| command, of course).  Be default, the \verb|save|
command uses an 8-digit exponential format notation to save the values to
the file.  You can specify that you want 16-digits using the
\begin{verbatim}
   save -ascii -double filename var1 var2
\end{verbatim}
form of the command.  Also, by default, \verb|save| uses spaces as the 
delimiters between the entries in the matrix.  If you want tabs instead,
you can use
\begin{verbatim}
   save -ascii -tabs filename var1 var2
\end{verbatim}
(you can also use both the \verb|-tabs| and \verb|-double| flags simultaneously).

Finally, you can specify that \verb|save| should only save variables that
match a particular regular expression.  Any of the above forms can be
combined with the \verb|-regexp| flag:
\begin{verbatim}
   save filename -regexp pattern1 pattern2
\end{verbatim}
in which case variables that match any of the patterns will be saved.
\subsection{Example}

Here is a simple example of \verb|save|/\verb|load|.  First, we save some 
variables to a file.
\begin{verbatim}
--> D = {1,5,'hello'};
--> s = 'test string';
--> x = randn(512,1);
--> z = zeros(512);
--> who
  Variable Name      Type   Flags             Size
              D      cell                    [1 3]
              s    string                    [1 11]
              x    double                    [512 1]
              z    double                    [512 512]
--> save loadsave.dat

\end{verbatim}
Next, we clear the variables, and then load them back from the file.
\begin{verbatim}
--> clear D s x z
--> who
  Variable Name      Type   Flags             Size
            ans    double                    [0 0]
--> load loadsave.dat
--> who
  Variable Name      Type   Flags             Size
              D      cell                    [1 3]
            ans    double                    [0 0]
              s    string                    [1 11]
              x    double                    [512 1]
              z    double                    [512 512]

\end{verbatim}
