MATLAB Function Reference |
Load workspace variables from disk
Syntax
load load('filename') load('filename', 'X', 'Y', 'Z') load('filename','-regexp'
, exprlist) load('-mat'
, 'filename') load('-ascii'
, 'filename') S = load(...) load filename-regexp
expr1 expr2 ...
Description
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',
loads those variables that match any of the regular expressions in '-regexp'
, exprlist)
exprlist
, where exprlist
is a comma-delimited list of quoted regular expressions.
load('
forces -mat
', 'filename')
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('
forces -ascii
', 'filename')
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
is the command form of the syntax.-regexp
expr1 expr2 ...
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,
Remarks
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
:
whos -file mydata.mat Name Size Bytes Class javArray 10x1 java.lang.Double[][] spArray 5x5 84 double array (sparse) strArray 2x5 678 cell array x 3x2x2 96 double array y 4x5 1230 cell array
Clear the workspace and load it from MAT-file mydata.mat
:
clear load mydata whos Name Size Bytes Class javArray 10x1 java.lang.Double[][] spArray 5x5 84 double array (sparse) strArray 2x5 678 cell array x 3x2x2 96 double array y 4x5 1230 cell array
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:
mydata mydata = 16.0000 2.0000 3.0000 13.0000 5.0000 11.0000 10.0000 8.0000 9.0000 7.0000 6.0000 12.0000 4.0000 14.0000 15.0000 1.0000 -5.7000 -5.7000 -5.7000 -5.7000 -5.7000 -5.7000 -5.7000 -5.7000 8.0000 6.0000 4.0000 2.0000
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
listdlg | loadobj |
© 1994-2005 The MathWorks, Inc.