load - Load variables from file into workspace - MATLAB (original) (raw)

Load variables from file into workspace

Syntax

Description

load([filename](#btm0etn-1-filename)) loads data fromfilename into the MATLAB® workspace. If filename is a MAT-file, thenload(filename) loads variables from the file; iffilename is an ASCII file, then load(filename) loads a double-precision array containing data from the file.

Note

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

example

load([filename](#btm0etn-1-filename),[variables](#btm0etn-1-variables)) loads the specified variables from the MAT-file filename.

example

load([filename](#btm0etn-1-filename),"-ascii") treatsfilename as an ASCII file, regardless of the file extension.

example

load([filename](#btm0etn-1-filename),"-mat") treatsfilename as a MAT-file, regardless of the file extension.

load([filename](#btm0etn-1-filename),"-mat",[variables](#btm0etn-1-variables)) loads the specified variables from filename.

`S` = load(___) loads data intoS, 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, thenS 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](#btm0etn-1-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.

View the contents of 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.

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.

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.

View the contents of the example 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: [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 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.

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 isfilename with .mat appended to it. Iffilename has an extension other than .mat, theload 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 infilename.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 infilename.Example: "C:\myFolder\myFile.mat"Example: "dataDir\myFile.mat"
Remote locations If the file is stored at a remote location, then specifyfilename 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 Location_schemeName_Amazon S3™s3Windows Azure® Blob Storagewasb, wasbsHDFS™hdfsFor more information on setting up MATLAB to access your online storage service, seeWork 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 Input Variables to Load
var1,var2,...,varN Load 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 withA.
"-regexp",expr1,expr2,...,exprN Load only the variables whose names match the regular expressions, specified as string scalars or character vectors. For example, load("filename.mat","-regexp","^Mon","^Tues") orload filename.mat -regexp ^Mon ^Tues loads only the variables in the file whose names begin with Mon orTues.

Limitations

Tips

Algorithms

If you do not specify an output when loading from an ASCII file, theload 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

expand all

Usage notes and limitations:

Version History

Introduced before R2006a

expand all

You can load data from v4, v6, and v7 MAT-files stored in remote locations, such as Amazon S3 and Windows Azure Blob Storage.

You can load data from v7.3 MAT-files stored in remote locations, such as Amazon S3 and Windows Azure Blob Storage.

See Also

Functions

Tools

Topics