Viewing the Contents of a MAT-File

MAT-files are binary MATLAB format files that store workspace variables.

To see the variables in a MAT-file before loading the file into your workspace, click the file name in the Current Folder browser. Information about the variables appears in the Details Panel.

Alternatively, use the command whos -file filename. This function returns the name, dimensions, size, and class of all variables in the specified MAT-file.

For example, view the contents of the demo file durer.mat:

whos -file durer.mat

MATLAB returns:

 Name           Size               Bytes  Class     Attributes

  X            648x509            2638656  double              
  caption        2x28                 112  char                
  map          128x3                 3072  double              

The byte counts represent the number of bytes that the data occupies when loaded into the MATLAB workspace. Compressed data uses fewer bytes in a file than in the workspace. In Version 7 or higher MAT-files, MATLAB compresses data. For more information, see MAT-File Versions.

Loading a MAT-File

To import all variables from a MAT-file, use one of the following options:

  • In the Current Folder browser, double-click the name of the file, or right-click the name of the file and select Open or Load.

Call the load function. For example, the following command loads all variables from the demo file durer.mat:

load('durer.mat')

To load variables into a structure array, specify an output variable for the load function:

durerStruct = load('durer.mat')

[warning]When you import data into the MATLAB workspace, the new variables you create overwrite any existing variables in the workspace that have the same name.[/warning]

Loading Selected Variables

To select and load MAT-file variables interactively, use any of the following options:

  • Select File > Import Data.
  • Click the Import data button in the Workspace browser toolbar.
  • Drag variables from the Details Panel of the Current Folder browser to the Workspace browser.

Alternatively, use the load function. For example, load variables X and map from durer.mat:

load('durer.mat', 'X', 'map')

To load variables with names that match a specific pattern, use one of the following options:

Include the wildcard character (*). For example, load all variables that start with str from a file named strinfo.mat:

load('strinfo.mat', 'str*')

Apply the -regexp option, which matches variables to regular expressions. For example, load all variables that start with Mon, Tue, or Wed from a hypothetical file named myfile.mat:

load('myfile.mat', '-regexp', '^Mon|^Tue|^Wed')

[important]For more information, see Regular Expressions in the MATLAB Programming Fundamentals documentation.[/important]

Troubleshooting: Loading Variables within a Function

If you define a function that loads data from a MAT-file, and find that MATLAB does not return the expected results, check whether any variables in the MAT-file share the same name as a MATLAB function. Common variable names that conflict with function names include i, j, mode, char, size, or path. To determine whether a particular name is associated with a MATLAB function, use the which function.

[tip]For example, consider a MAT-file with variables height, width, and length. If you load these variables using a function such as findVolume,[/tip]

function vol = findVolume(myfile)
  load(myfile);
  vol = height * width * length;

MATLAB interprets the reference to length as a call to the length function, and returns an error:

??? Error using ==> length
Not enough input arguments.

To avoid confusion, when defining your function, choose one (or more) of the following approaches:

Load into a structure array. For example, define the findVolume function as follows:

function vol = findVolume(myfile)
  dims = load(myfile);
  vol = dims.height * dims.width * dims.length;
  • Explicitly include the names of variables in the call to load, as described in Loading Selected Variables.
  • Initialize variables within the function before calling load.

Any of these methods allow you to override the function name with the variable name within your function. For more information, see Potential Conflict with Function Names in the MATLAB Programming Fundamentals documentation.