| MATLAB® | ![]() |
| On this page… |
|---|
To import variables from a binary or ASCII file on your disk to your workspace, use the load function. You can load all variables from the workspace in a single operation (if you omit the filename, the MATLAB® software loads from file matlab.mat):
load filename
or load just those variables that you specify:
load filename var1 var2 ... varN
Use the wildcard character (*) in the variable name to load those variables that match a specific pattern. (This works for MAT-files only.) For example, the following command loads all variables that start with str from file strinfo.mat:
load strinfo str*
Caution When you import data into the MATLAB workspace, it overwrites any existing variable in the workspace with the same name. |
To see what variables are stored in a MAT-file before actually loading the file into your workspace, use whos -file filename. This command returns the name, dimensions, size, and class of all variables in the specified MAT-file.
You can use whos -file on binary MAT-files only:
whos -file mydata.mat Name Size Bytes Class javArray 10x1 java.lang.Double[][] spArray 5x5 84 double array (sparse) strArray 2x5 678 cell array x 3x2x2 96 double array y 4x5 1230 cell array
To load MAT-file data into a MATLAB structure, specify an output variable in your load command. This example reads the data in mydata.mat into the fields of structure S:
S = load('mydata.mat')
S =
x: [3x2x2 double]
y: {4x5 cell}
spArray: [5x5 double]
strArray: {2x5 cell}
javArray: [10x1 java.lang.Double[][]]
whos S
Name Size Bytes Class
S 1x1 2840 struct arrayMAT-files are double-precision binary MATLAB format files created by the save function and readable by the load function. You can create MAT-files on one machine and later MATLAB can read them on another machine with a different floating-point format, retaining as much accuracy and range as the different formats allow. Other programs, external to MATLAB, can also manipulate MAT-files.
MAT-files can contain data in an uncompressed or a compressed form, or both. MATLAB knows which variables in the file have been compressed by looking at a tag that it attaches to each variable during the save operation. When loading data from a MAT-file into the workspace, MATLAB automatically handles the decompression of the appropriate data.
The External Interface libraries contain C- and Fortran-callable routines to read and write MAT-files from external programs.
ASCII files must be organized as a rectangular table of numbers, with each number in a row separated by a blank, comma, or tab character, and with an equal number of elements in each row. MATLAB generates an error if the number of values differs between any two rows. ASCII files can contain MATLAB comments (lines that begin with %).
MATLAB returns all the data in the file as a single two-dimensional array of type double. The number of rows in the array is equal to the number of lines in the file, and the number of columns is equal to the number of values on a line.
In the workspace, MATLAB assigns the array to a variable named after the file being loaded (minus any file extension). For example, the command
load mydata.dat
reads all of the data from mydata.dat into the MATLAB workspace as a single array, and assigns it to a variable called mydata. In naming the variable, 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
assigns the data in file 10-May-data.dat to a new workspace variable called X10_May_data.
![]() | Exporting Data to MAT-Files | Importing Text Data | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |