Documentation Center

  • Trial Software
  • Product Updates

Load Parts of Variables from MAT-Files

Load Using the matfile Function

This example shows how to load part of a variable from an existing MAT-file.


A = rand(5);
B = magic(10);
save example.mat A B -v7.3;
clear A B

Construct a object that can load parts of variables from the file, example.mat.

example = matfile('example.mat')
example =

      Properties.Source: C:\Documents\MATLAB\example.mat
    Properties.Writable: false           
                      A: [5x5   double]  
                      B: [10x10 double]  

The matfile function creates a object that corresponds to a MAT-file and displays the properties of the object.

Load the first column of B from example.mat into variable firstColB.

firstColB = example.B(:,1);

When you index into objects associated with Version 7.3 MAT-files, MATLAB® loads only the part of the variable that you specify.

By default, matfile only allows loading from existing MAT-files. To enable saving, call matfile with the Writable parameter.


Alternatively, construct the object and set Properties.Writable in separate steps.

example = matfile('example.mat');
example.Properties.Writable = true;

Load from Variables with Unknown Names

This example shows how to dynamically access variables, whose names are not always known. Consider the example MAT-file, topography.mat, that contains one or more arrays with unknown names.

Construct a object that corresponds to the file, topography.mat. Call who to get the variable names in the file.

matObj = matfile('topography.mat');
varlist = who(matObj)
varlist = 


varlist is a cell array containing the names of the four variables in topography.mat.

The third and fourth variables, topomap1 and topomap2, are both arrays containing topography data. Load the elevation data from the third column of each variable into a field of the structure array, S. For each field, specify a field name that is the original variable name prefixed by elevationOf_. Access the data in each variable as properties of matObj. Because varName is a variable, enclose it in parentheses.

for index = 3:4
    varName = varlist{index};
    S(1).(['elevationOf_',varName]) = matObj.(varName)(:,3);

View the contents of the structure array, S.

S = 

    elevationOf_topomap1: [64x1 double]
    elevationOf_topomap2: [128x1 double]

S has two fields, elevationOf_topomap1 and elevationOf_topomap2, each containing a column vector.

Avoid Repeated File Access

The primary advantage of matfile over the load function is that you can process parts of very large data sets that are otherwise too large to fit in memory. When working with these large variables, read and write as much data into memory as possible at a time. Otherwise, repeated file access negatively impacts the performance of your code.

For example, suppose a variable in your file contains many rows and columns, and loading a single row requires most of the available memory. To calculate the mean of the entire data set, calculate the mean of each row, and then find the overall mean.

example = matfile('example.mat');
[nrows, ncols] = size(example,'B');

avgs = zeros(1, nrows);
for idx = 1:nrows
    avgs(idx) = mean(example.B(idx,:));
overallAvg = mean(avgs);

Avoid Inadvertently Loading Entire Variables

When you do not know the size of a large variable in a MAT-file, and want to load parts of that variable at a time, do not use the end keyword. Rather, call the size method for objects. For example, this code

lastColB = example.B(:,ncols);

requires less memory than

lastColB = example.B(:,end);

which temporarily loads the entire contents of B. For very large variables, loading takes a long time or generates Out of Memory errors.

Similarly, any time you refer to a variable with syntax of the form matObj.varName, such as example.B, MATLAB temporarily loads the entire variable into memory. Therefore, make sure to call the size method for objects with syntax such as


rather than passing the entire contents of example.B to the size function,

[nrows,ncols] = size(example.B);

The difference in syntax is subtle, but significant.

Partial Loading Requires Version 7.3 MAT-Files



newfile = matfile('newfile.mat');

creates a MAT-file that supports partial loading and saving.




Was this topic helpful?