Quantcast

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.

To run the code in this example, create a Version 7.3 MAT-file with two variables.

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

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

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

  matlab.io.MatFile

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

The matfile function creates a matlab.io.MatFile object that corresponds to a MAT-file and displays the properties of the matlab.io.MatFile 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.

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

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 matlab.io.MatFile 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 = 

    'topo'
    'topolegend'
    'topomap1'
    'topomap2'

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);
end

View the contents of the structure array, S.

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,:));
end
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 matlab.io.MatFile objects. For example, this code

[nrows,ncols] = size(example,'B');
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 matlab.io.MatFile objects with syntax such as

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

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

Any load or save operation that uses a matlab.io.MatFile object associated with a Version 7 or earlier MAT-file temporarily loads the entire variable into memory.

The matfile function creates files in Version 7.3 format. For example, this code

newfile = matfile('newfile.mat');

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

However, by default, the save function creates Version 7 MAT-files. Convert existing MAT-files to Version 7.3 by calling the save function with the -v7.3 option, such as

load('durer.mat');
save('mycopy_durer.mat','-v7.3');

To change your preferences to save new files in Version 7.3 format, access the Environment section on the Home tab, and click Preferences. Select MATLAB > General > MAT-Files.

Was this topic helpful?