Main Content

load

Load variables from file into workspace

Description

load(filename) loads data from filename into the MATLAB® workspace. If filename is a MAT-file, then load(filename) loads variables from the file; if filename is an ASCII file, then load(filename) loads a double-precision array containing data from the file.

Note

Security Considerations: The load command might execute code contained in a MAT-file as it initializes variables. Avoid calling load on untrusted MAT-files.

example

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

example

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

example

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 argument combinations in previous syntaxes. If filename is a MAT-file, then S is a structure array; if filename is an ASCII file, then S is an m-by-n double-precision array containing data from the file, where m is the number of lines in the file and n is the number of values on each line.

example

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

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

load test.mat    % command form
load("test.mat") % function form

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

load 'my file.mat' X    % command form, using single quotes
load("my file.mat","X") % function form, using double quotes

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

example

Examples

collapse all

Load all variables from the MAT-file gong.mat into the MATLAB workspace.

First, check the contents of the workspace.

whos

View the contents of gong.mat.

whos("-file","gong.mat")
  Name          Size             Bytes  Class     Attributes

  Fs            1x1                  8  double              
  y         42028x1             336224  double              

Load gong.mat and then check the contents of the workspace again.

load("gong.mat")
whos
  Name          Size             Bytes  Class     Attributes

  Fs            1x1                  8  double              
  y         42028x1             336224  double              

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.

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

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              2748  cell                
  hwycols           1x1                 8  double              
  hwydata          51x17             6936  double              
  hwyheaders        1x17             2894  cell                
  hwyidx           51x1               408  double              
  hwyrows           1x1                 8  double              
  statelabel       51x1              7004  cell                
  ushwydata         1x17              136  double              
  uslabel           1x1               146  cell                

Use function syntax to load all variables with names that do not begin 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"};

Load the selected variables from durer.mat into a structure array.

S = load(filename,myVars{:})
S = struct with fields:
          X: [648x509 double]
    caption: [2x28 char]

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

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

a = magic(4);
b = -5.7*ones(2,4);
c = [8 6 4 2];
save mydata.dat a b c -ascii
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 string scalar or character vector. If you do not specify filename, the load function searches for a file named matlab.mat.

If filename has no extension (that is, does not end with a period followed by text), load searches for a file whose name is filename with .mat appended to it. If filename has an extension other than .mat, the load function treats the file as ASCII data.

Note

ASCII files must contain a rectangular table of decimal 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, %).

Depending on the location of your file, filename can be in one of these forms.

Location

Form

Current folder or folder on the MATLAB path

Specify the name of the file in filename.

Example: "myFile.mat"

File in a folder

If the file is not in the current folder or in a folder on the MATLAB path, then specify the full or relative path in filename.

Example: "C:\myFolder\myFile.mat"

Example: "dataDir\myFile.mat"

Remote locations

If the file is stored at a remote location, then specify filename as a uniform resource locator (URL) of this form:

schemeName://pathToFile/fileName.mat

Based on your remote location, schemeName can be one of the values in this table.

Remote LocationschemeName
Amazon S3™s3
Windows Azure® Blob Storagewasb, wasbs
HDFS™hdfs

For more information on setting up MATLAB to access your online storage service, see Work with Remote Data.

Example: "s3://myBucket/myPath/myFile.mat"

When using the command form of load, you do not need to enclose the input in single or double quotes. However, if filename contains a space, you must enclose the argument in single quotes. For example, load 'filename withspace.mat'.

Names of variables to load, specified as one or more string scalars or character vectors. When using the command form of load, you do not need to enclose the input in single quotes.

variables can be in one of these forms.

Form of variables InputVariables to Load
var1,var2,...,varNLoad the listed variables, specified as individual string scalars or 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,expr2,...,exprNLoad only the variables whose names match the regular expressions, specified as string scalars or 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.

Limitations

  • When working with remote data, load does not support treating the input file as an ASCII file.

Tips

  • You can use these strategies to speed up loading of MAT-files from network drives:

    • Use the copyfile function to copy the file from the network drive to a local drive before applying the load function to the local copy.

    • Use the matfile function to access the file without loading it into the workspace.

    • Decrease or disable refreshing of the Current Folder browser. To do this, go to the Home tab, and in the Environment section, select Preferences. Select MATLAB > Current Folder. You can either increase the Number of seconds between auto-refresh value to a number greater than the default value of 3, or clear the Auto-refresh view from file system check box to disable the feature.

Algorithms

If you do not specify an output when loading from an ASCII file, the load function creates a variable named after the loaded file (minus any file extension). For example, the command load mydata.dat reads data into a variable named mydata. For example, see Load ASCII File

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 named X10_May_data.

Extended Capabilities

Version History

Introduced before R2006a

expand all