This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Load variables from file into workspace


  • load(filename)
  • load(filename,variables)
  • load(filename,'-ascii')
  • load(filename,'-mat')
  • load(filename,'-mat',variables)



load(filename) loads data from filename.

  • If filename is a MAT-file, then load(filename) loads variables in the MAT-File into the MATLAB® workspace.

  • If filename is an ASCII file, then load(filename) creates a double-precision array containing data from the file.


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


load(filename,'-ascii') treats filename as an ASCII file, regardless of the file extension.

load(filename,'-mat') treats filename as a MAT-file, regardless of the file extension.

load(filename,'-mat',variables) loads the specified variables from filename.


S = load(___) loads data into S, using any of the input arguments in the previous syntax group.

  • If filename is a MAT-file, then S is a structure array.

  • If filename is an ASCII file, then S is a double-precision array containing data from the file.


load filename is the command form of the syntax. Command form requires fewer special characters. You do not need to type parentheses or enclose input in single quotes. Separate inputs with spaces instead of commas.

For example, to load a file named durer.mat, these statements are equivalent:

load durer.mat      % command form
load('durer.mat')   % function form

You can include any of the inputs described in previous syntaxes. For example, to load the variable named X:

load durer.mat X       % command form
load('durer.mat','X')  % function form

Do not use command form when any of the inputs, such as filename, are variables.


collapse all

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

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

disp('Contents of gong.mat:')

disp('Contents of workspace after loading file:')

You also can use command syntax to load the variables. Clear the previously loaded variables and repeat the load operation.

clear y Fs

load gong.mat

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


You also can use command syntax to load the variable, y.

load handel.mat y

View the contents of the example file, accidents.mat.

whos -file accidents.mat
 Name              Size            Bytes  Class     Attributes

  datasources       3x1              2724  cell                
  hwycols           1x1                 8  double              
  hwydata          51x17             6936  double              
  hwyheaders        1x17             2758  cell                
  hwyidx           51x1               408  double              
  hwyrows           1x1                 8  double              
  statelabel       51x1              6596  cell                
  ushwydata         1x17              136  double              
  uslabel           1x1               138  cell                

Use function syntax to load all variables with names not beginning with 'hwy', from the file.

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

Alternatively, use command syntax to load the same variables.

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

The file, durer.mat, contains variables X, caption, and map. Create a cell array of variable names to load.

filename = 'durer.mat';
myVars = {'X','caption'};
S = load(filename,myVars{:})
S = 

  struct with fields:

          X: [648×509 double]
    caption: [2×28 char]

Only the variables X and caption are loaded into the structure array, S.

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

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 -ascii

load creates an array of type double named mydata.

View information about mydata.

whos mydata
  Name        Size            Bytes  Class     Attributes

  mydata      7x4               224  double              

Input Arguments

collapse all

Name of file, specified as a character vector. 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.

When using the command form of load, it is unnecessary to enclose the input in single quotes. However, if filename contains a space, you must enclose the argument in single quotes. For example, load 'filename withspace.mat'.

ASCII files 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, %).

Example: 'myFile.mat'

Data Types: char

Names of variables to load, specified as one or more character vector. When using the command form of load, you do not need to enclose the input in single quotes. variables can be in one of the following forms.

Form of variables InputVariables to Load
var1,...,varNLoad the listed variables, specified as individual character vectors.
Use the '*' wildcard to match patterns. For example, load('filename.mat','A*') or load filename.mat A* loads all variables in the file whose names start with A.
'-regexp',expr1,...,exprNLoad only the variables or fields whose names match the regular expressions, specified as character vectors. For example, load('filename.mat','-regexp','^Mon','^Tues') or load filename.mat -regexp ^Mon ^Tues loads only the variables in the file whose names begin with Mon or Tues.

Data Types: char

Output Arguments

collapse all

Loaded variables, returned as a structure array, if filename is a MAT-File.

Loaded data, returned as an m-by-n array of type double, if filename is an ASCII file. m is equal to the number of lines in the file, and n is equal to the number of values on a line.

More About

collapse all


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.

Introduced before R2006a

Was this topic helpful?