This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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 object that can load parts of variables from the file, example.mat.

example = matfile('example.mat') ;

The matfile function creates a object that corresponds to a MAT-file and contains the properties of the object. To display the object properties, type example.Properties in the command window.

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 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 =

  4×1 cell array


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 = 

  struct with fields:

    elevationOf_topomap1: [64×1 double]
    elevationOf_topomap2: [128×1 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

[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 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 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


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?