Documentation

matfile

Access and change variables directly in MAT-files, without loading into memory

Syntax

m = matfile(filename)
m = matfile(filename,'Writable',isWritable)

Description

example

m= matfile(filename)creates a MAT-file object,m, connected to the MAT-file namedfilename. The object allows you to access and change variables directly in a MAT-file, without having to load the variables into memory.

The partial loading and saving that thematfilefunction provides requires less memory than theloadandsavecommands, which always operate on entire variables.

example

m= matfile(filename,'Writable',isWritable)enables or disables write access to the file.

Examples

collapse all

负载变量topofrom the example file,topography.mat.

Open the example MAT-file,topography.mat.

filename ='topography.mat'; m = matfile(filename);

Read the variabletopofrom the MAT-file.

topo = m.topo;

MATLAB® loads the entire variable,topo, into the workspace.

Generate a 20-by-20 example array,x, and save it to a MAT-file calledmyFile.mat.

x = magic(20); save('myFile.mat','x');

Create a MAT-file object connected to the existing MAT-file namedmyFile.mat. Enable write access to the MAT-file by settingWritabletotrue.

m = matfile('myFile.mat','Writable',true);

Generate a 15-by-15 example array,y.

y = magic(15);

Saveyto the MAT-file. Specify the variable in the MAT-file using dot notation similar to accessing fields of structure arrays.

m.y = y;

MATLAB® adds a variable namedyto the file.

Display all variables stored in the MAT-file,myFile.mat.

whos('-file','myFile.mat')
Name Size Bytes Class Attributes x 20x20 3200 double y 15x15 1800 double

Access specific elements of a MAT-file variable.

Open a new MAT-file,myFile2.mat.

m = matfile('myFile2.mat');

Save a 20-by-20 example array to part of a variable,y, inmyFile2.mat. Specify the variable in the MAT-file using dot notation similar to accessing fields of structure arrays.

m.y(81:100 81:100) =魔法(20);

MATLAB®插入the 20-by-20 array into the elements ofyspecified by the indices(81:100,81:100).

Read a subset of arrayyinto a new workspace variable,z.

z = m.y(85:94,85:94);

MATLAB reads the 10-by-10 subarray specified by the indices(85:94,85:94)from the MAT-file into workspace variablez.

Determine the size of a variable, and then calculate the average of each column.

Open the example MAT-file,stocks.mat.

filename ='stocks.mat'; m = matfile(filename);

Determine the size of the variable,stocks, instocks.mat.

[nrows,ncols] = size(m,'stocks');

Compute the average of each column of the variablestocks.

avgs = zeros(1,ncols);fori = 1:ncols avgs(i) = mean(m.stocks(:,i));end

Enable write access to the MAT-file,myFile.mat, by settingWritabletotruewhen you open the MAT-file.

filename ='myFile.mat'; m = matfile(filename,'Writable',true);

Alternatively, setProperties.Writablein a separate step after you open the MAT-file.

m.Properties.Writable = true;

Input Arguments

collapse all

Name of a MAT-file, specified as a character vector. If the file is not in the current folder,filenamemust include a full or a relative path. Iffilenamedoes not include an extension, thenmatfileappends.mat.

If the file does not exist, thenmatfilecreates a Version 7.3 MAT-file on the first assignment to a variable.

matfileonly supports efficient partial loading and saving for MAT-files in Version 7.3 format. If you index into a variable in a Version 7 (the current default) or earlier MAT-file, MATLAB®warns and temporarily loads the entire contents of the variable.

Example:'myFile.mat'

Data Types:char

Write access to the MAT-file, specified as eithertrueorfalse.

  • trueenables saving to the MAT-file.If the file is read only, MATLAB changes the system permissions with thefileattribfunction.

  • falsedisables saving to the MAT-file. MATLAB does not change the system permissions.

The default value istruefor new files, andfalsefor existing files.

Data Types:logical

Output Arguments

collapse all

MAT-file object connected to a MAT-file.

Access variables in the MAT-file with dot notation similar to accessing fields of structure arrays:

  • To load part of variablevarNamefrom the MAT-file corresponding tom, call:

    loadedData = m.varName(indices);

  • To save part of variablevarNameto the MAT-file corresponding tom, call:

    m.varName(indices) = dataToSave;

When accessing variables, specify indices for all dimensions. Indices can be a single value, an equally spaced range of increasing values, or a colon(:); for example:

m.varName(100:500,200:600) m.varName(:,501:1000) m.varName(1:2:1000,80)

Limitations

  • matfiledoes not support linear indexing. You must specify indices for all dimensions.

  • matfiledoes not support indexing into:

    • Variables of tables

    • Cells of cell arrays

    • Fields of structure arrays

    • User-defined classes

    • Sparse arrays

  • You cannot assign complex values to an indexed portion of a real array.

  • You cannot evaluate function handles using themoutput. For example, if your MAT-file contains function handlemyfunc, the syntaxm.myfunc()attempts to index into the function handle, and does not invoke the function.

  • Efficient partial loading and saving requires Version 7.3 MAT-files. To create a Version 7.3 MAT-file, call thesavefunction with the'-v7.3'option. For example, to convert an existing MAT-file nameddurer.matto Version 7.3, call:

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

Tips

  • Using theendkeyword as part of an index causes MATLAB to load the entire variable into memory. For very large variables, this load operation results inOutofMemoryerrors. Rather than usingend, determine the extent of a variable,myVar, with thesizemethod, such as:

    sizeMyVar = size(m,'myVar')

Introduced in R2011b

Was this topic helpful?