load

Loads variables from a MAT-file or numeric data from a text file.

Category: Data Import and Analysis > Data Import and Export

Syntax

load(filename)

Loads all variables of a MAT-file into the workspace.

load(filename, var1, var2)

Loads only the named variables.

S = load(filename)

Returns the contents in a struct instead of creating variables.

load(filename, '-ascii')

Reads a numeric text file; the variable is named after the file.

Description

Prefer the form with an output, S = load(...): it never overwrites existing variables and makes it clear where a name came from. Loading a text file requires that every line has the same number of numbers.

Input arguments

filename
File name. .mat is assumed when there is no extension.
var1, ...
Names of variables to load.

Output arguments

S
Struct with one field per loaded variable; for a numeric text file, the matrix itself.

Example

Save, clear and load back

x = 1:3; s = 'hi';
save('v.mat', 'x', 's');
clear x s
S = load('v.mat')
load('v.mat', 'x');
x
Output
S = struct with fields:

    s: 'hi'
    x: [1 2 3]
x = 1×3

     1     2     3

See also

save matfile importdata readmatrix fileread