Programming Previous page   Next Page

Importing Data from MAT-Files

This section covers

Using the load Function

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, MATLAB loads from file matlab.mat):

or load just those variables that you specify:

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:

Previewing MAT-File Contents

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 data type of all variables in the specified MAT-file.

You can use whos -file on binary MAT-files only:

Loading into a Structure

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:

Loading Binary Data

MAT-files are double-precision binary MATLAB format files created by the save function and readable by the load function. They can be created on one machine and later read by MATLAB on another machine with a different floating-point format, retaining as much accuracy and range as the different formats allow. They can also be manipulated by other programs, external to MATLAB.

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.

Loading ASCII Data

ASCII files must be organized as a rectangular table of numbers, with each number in a row separated by a blank 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

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

assigns the data in file 10-May-data.dat to a new workspace variable called X10_May_data.

Previous page  Saving and Loading MAT-Files Accessing Files with Memory-Mapping Next page

© 1994-2005 The MathWorks, Inc.