Skip to Main Content Skip to Search
Product Documentation

load - Load data from MAT-file into workspace

Syntax

S = load(filename)
S = load(filename, variables)
S = load(filename, '-mat', variables)
S = load(filename, '-ascii')
load(filename, ...)
load filename ...

Description

S = load(filename) loads the variables from a MAT-file into a structure array, or data from an ASCII file into a double-precision array.

S = load(filename, variables) loads the specified variables from a MAT-file.

S = load(filename, '-mat', variables) forces load to treat the file as a MAT-file, regardless of the extension. Specifying variables is optional.

S = load(filename, '-ascii') forces load to treat the file as an ASCII file, regardless of the extension.

load(filename, ...) loads without combining MAT-file variables into a structure array.

load filename ... is the command form of the syntax, for convenient loading from the command line. With command syntax, you do not need to enclose input strings in single quotation marks. Separate inputs with spaces instead of commas. Do not use command syntax if inputs such as filename are variables. For more information, see Command vs. Function Syntax in the MATLAB Programming Fundamentals documentation.

Input Arguments

filename

Name of a file. If you do not specify filename, the load function searches for a file named matlab.mat.

filename can include a file extension and a full or partial path. If filename has no extension (that is, no text after a period), load looks for a file named filename.mat. If filename has an extension other than .mat, the load function treats the file as ASCII data.

Default: 'matlab.mat'

variables

List of variables to load from the file. Valid for MAT-files only. Use one of the following forms:

var1, var2, ...Load the listed variables.
Use the '*' wildcard to match patterns. For example, load('A*') loads all variables that start with A.
'-regexp', expressionsLoad only the variables or fields that match the specified regular expressions.

Default: all variables in file

'-mat'

Keyword that indicates that the specified file is a MAT-file, regardless of the extension.

'-ascii'

Keyword that indicates that the specified file is an ASCII file, regardless of the extension.

The following remarks apply to loading all ASCII files, even when you do not include the -ascii keyword, but load a file with an extension other than .mat:

  • The file must contain a rectangular table of numbers, with an equal number of elements in each row. The file delimiter (the character between elements in each row) can be a blank, comma, semicolon, or tab character. The file can contain MATLAB comments (lines that begin with a percent sign, %).

  • MATLAB returns the data as a single two-dimensional array of type double. The number of rows in the array is equal to the number of lines in the file. The number of columns is equal to the number of values on a line.

  • If you do not specify an output for the load function, MATLAB creates a variable named after the loaded file (minus any file extension). For example, the command

    load mydata.dat
    

    reads data into a variable called mydata.

    To create the variable name, load precedes any leading underscores or digits in filename with an X and replaces any other nonalphabetic characters with underscores. For example, the command

    load 10-May-data.dat
    

    creates a variable called X10_May_data.

Examples

Load all variables from the demo MAT-file gong.mat. Check the contents of the workspace before and after the load operation.

disp('Contents of workspace before loading file:')
whos

disp('Contents of gong.mat:')
whos -file gong.mat

load gong.mat
disp('Contents of workspace after loading file:')
whos
 

Load only the variable y from the demo file handel.mat. If the workspace already contains a variable y, the load operation overwrites it with data from the file.

load('handel.mat', 'y')
 

Create a list of variables to load using a cell array. Call load using function syntax.

%This demo file contains variables X, caption, and map.
demoFile = 'durer.mat';

% Load selected variables.
vars = {'X', 'caption'};
load(demoFile, vars{:});
 

Use regular expressions to load four variables from the demo file accidents.mat that do not begin with 'hwy':

load('accidents.mat', '-regexp', '^(?!hwy)...');
 

Load the first three variables from accidents.mat, determined alphabetically by variable name:

vars = whos('-file', 'accidents.mat');
load('accidents.mat', vars(1:3).name);
 

Create an ASCII file from several 4-column matrices, and load the data back into a double array named mydata:

a = magic(4);
b = ones(2, 4) * -5.7;
c = [8 6 4 2];
save -ascii mydata.dat a b c
clear a b c

load mydata.dat

Alternatives

The easiest way to import data is to use the Import Wizard, a graphical user interface. The Import Wizard imports both MAT-files and ASCII files, and provides direct control over the variables to create. To start the Wizard, select File > Import Data or call uiimport.

See Also

clear | importdata | matfile | regexp | save | uiimport | whos

How To

  


» Learn more
» Download free kit
» Get trial software

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS