FreeMat: LOAD Load Variables From A File

FreeMat

- Main Page
- Related Pages

LOAD Load Variables From A File

Section: Input/Ouput Functions

# Usage

Loads a set of variables from a file in a machine independent format. The `load` function takes one argument:

      load filename,

or alternately,

      load('filename')

This command is the companion to `save`. It loads the contents of the file generated by `save` back into the current context. Global and persistent variables are also loaded and flagged appropriately. By default, FreeMat assumes that files that end in a `.mat` or `.MAT` extension are MATLAB-formatted files. Also, FreeMat assumes that files that end in `.txt` or `.TXT` are ASCII files. For other filenames, FreeMat first tries to open the file as a FreeMat binary format file (as created by the `save` function). If the file fails to open as a FreeMat binary file, then FreeMat attempts to read it as an ASCII file.

You can force FreeMat to assume a particular format for the file by using alternate forms of the `load` command. In particular,

      load -ascii filename

will load the data in file `filename` as an ASCII file (space delimited numeric text) loaded into a single variable in the current workspace with the name `filename` (without the extension).

For MATLAB-formatted data files, you can use

      load -mat filename

which forces FreeMat to assume that `filename` is a MAT-file, regardless of the extension on the filename.

You can also specify which variables to load from a file (not from an ASCII file - only single 2-D variables can be successfully saved and retrieved from ASCII files) using the additional syntaxes of the `load` command. In particular, you can specify a set of variables to load by name

      load filename Var_1 Var_2 Var_3 ...

where `Var_n` is the name of a variable to load from the file. Alternately, you can use the regular expression syntax

      load filename -regexp expr_1 expr_2 expr_3 ...

where `expr_n` is a regular expression (roughly as expected by `regexp`). Note that a simpler regular expression mechanism is used for this syntax than the full mechanism used by the `regexp` command.

Finally, you can use `load` to create a variable containing the contents of the file, instead of automatically inserting the variables into the curent workspace. For this form of `load` you must use the function syntax, and capture the output:

      V = load('arg1','arg2',...)

which returns a structure `V` with one field for each variable retrieved from the file. For ASCII files, `V` is a double precision matrix.

# 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
                  D      cell                    [1x3]
                  s      char                    [1x11]
                  x    double                    [512x1]
                  z    double                    [512x512]
    --> save loadsave.dat

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

    --> clear D s x z
    --> who
      Variable Name       Type   Flags             Size
                ans    double                    [0x0]
    --> load loadsave.dat
    --> who
      Variable Name       Type   Flags             Size
                  D      cell                    [1x3]
                ans    double                    [0x0]
                  s      char                    [1x11]
                  x    double                    [512x1]
                  z    double                    [512x512]