MATLAB Function Reference Previous page   Next Page

Load workspace variables from disk



load loads all the variables from the MAT-file matlab.mat, if it exists, and returns an error if it doesn't exist.

load('filename') loads all the variables from filename given a full pathname or a MATLABPATH relative partial pathname. If filename has no extension, load looks for a file named filename.mat and treats it as a binary MAT-file. If filename has an extension other than .mat, load treats the file as ASCII data.

load('filename', 'X', 'Y', 'Z') loads just the specified variables from the MAT-file. The wildcard '*' loads variables that match a pattern (MAT-file only).

load('filename', '-regexp', exprlist) loads those variables that match any of the regular expressions in exprlist, where exprlist is a comma-delimited list of quoted regular expressions.

load('-mat', 'filename') forces load to treat the file as a MAT-file, regardless of file extension. If the file is not a MAT-file, load returns an error.

load('-ascii', 'filename') forces load to treat the file as an ASCII file, regardless of file extension. If the file is not numeric text, load returns an error.

S = load(...) returns the contents of a MAT-file in the variable S. If the file is a MAT-file, S is a struct containing fields that match the variables retrieved. When the file contains ASCII data, S is a double-precision array.

load filename -regexp expr1 expr2 ... is the command form of the syntax.

Use the functional form of load, such as load('filename'), when the file name is stored in a string, when an output argument is requested, or if filename contains spaces. To specify a command-line option with this functional form, specify any option as a string argument, including the hyphen. For example,


For information on any of the following topics related to saving to MAT-files, see Importing Data from MAT-Files in the "MATLAB Programming" documentation:

You can also use the Current Directory browser to view the contents of a MAT-file without loading it--see Viewing Information About M-Files and MAT-Files.


Example 1 -- Loading From a Binary MAT-file

To see what is in the MAT-file prior to loading it, use whos -file:

Clear the workspace and load it from MAT-file mydata.mat:

Example 2 -- Loading From an ASCII File

Create several 4-column matrices and save them to an ASCII file:

Clear the workspace and load it from the file mydata.dat. If the filename has an extension other than .mat, MATLAB assumes that it is ASCII:

MATLAB loads all data from the ASCII file, merges it into a single matrix, and assigns the matrix to a variable named after the filename:

Example 3 -- Using Regular Expressions

Using regular expressions, load from MAT-file mydata.mat those variables with names that begin with Mon, Tue, or Wed:

Here is another way of doing the same thing. In this case, there are three separate expression arguments:

See Also

clear, fprintf, fscanf, partialpath, save, spconvert, who

Previous page  listdlg loadobj Next page

© 1994-2005 The MathWorks, Inc.