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.
load([filename](#btm0etn-1-filename),[variables](#btm0etn-1-variables))
loads the specified variables from the MAT-file filename
.
load([filename](#btm0etn-1-filename),"-ascii")
treatsfilename
as an ASCII file, regardless of the file extension.
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.
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.
Examples
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
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
- 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 Files panel. To do this, go to theHome tab, and in the Environment section, select Settings. Select > . 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.
- Use the copyfile function to copy the file from the network drive to a local drive before applying the
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
Usage notes and limitations:
- Use
load
only when generating MEX functions or code for Simulink® simulation. To load compile-time constants, usecoder.load
. - Code generation does not support use of the
load
function without assignment of the loaded data to a structure or array. For example, useS = load(filename)
, notload(filename)
. - The output
S
must be the name of a structure or array without any subscripting. For example,S(i) = load("myFile.mat")
is not supported. - The filename passed to
load
must be a character vector or string scalar that is constant at code generation time. All other arguments toload
must be character vector or string scalar literals. - If the MAT-file contains unsupported constructs, use
S = load(filename,variables)
to load only the supported constructs. - When using the
load
function to load variables whose size can change at run time, you must explicitly declare the variables as variable-size data by usingcoder.varsize
. - The code generator automatically treats
load
as an extrinsic function, meaning that C/C++ code is not generated for theload
call body. The generated code instead dispatches theload
call to the MATLAB engine for execution. There are therefore certain restrictions on the types of variables that you can load using theload
function. SeeRestrictions on Using Extrinsic Functions (MATLAB Coder). - Although the data types of the variables loaded using the
load
function must remain the same between code generation time and run time, the actual values of these variables may change and will be read at run time.
Version History
Introduced before R2006a
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.