matfile
Access and change variables in MAT-file without loading file into memory
Description
Use a MAT-file object to access and change variables in a MAT-file without loading the file into memory. You can load or save parts of variables. Partial loading and saving of variables using a MAT-file object requires less memory than the load
and save
commands.
Creation
Description
creates a matObj
= matfile(filename
)matlab.io.MatFile
object connected to the MAT-file specified by
filename
.
The MAT-file object allows you to access and change variables directly in a MAT-file, without having to load the variables into memory.
enables or disables write access to the file. Specify matObj
= matfile(filename
,"Writable",isWritable
)isWritable
as
true
or false
.
Input Arguments
Properties
Object Functions
size | Get array dimensions of variable in MAT-file
Note: Do not call |
who | Get list of variables in MAT-file
|
whos | Get list of variables in MAT-file, with sizes and types
|
Examples
Limitations
When accessing parts of variables in MAT-files by indexing:
Linear indexing is not supported. You must specify indices for all dimensions.
Assigning complex values to indexed portions of a real array is not supported.
For sparse arrays in a MAT-file, the MAT-file object:
Supports reading a subset of a sparse array by indexing.
Does not support writing (assigning values) to a subset of a sparse array by indexing.
The MAT-file object does not support indexing into:
Variables of tables
Cells of cell arrays
String arrays
datetime
arraysduration
arraysFields of structure arrays
User-defined classes
The MAT-file object does not support evaluating function handles using the
m
output. 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.When saving a handle object, MATLAB saves a copy of the object, not a copy of the handle. Therefore, modifying the object via its handle in the workspace from which the object was saved does not modify the saved handle object.
Tips
Efficient partial loading and saving requires Version 7.3 MAT-files. To create a Version 7.3 MAT-file, call the
save
function with the'-v7.3'
option. For example, to convert an existing MAT-file nameddurer.mat
to Version 7.3, call:load('durer.mat'); save('mycopy_durer.mat','-v7.3');
Using the
end
keyword as part of an index causes MATLAB to load the entire variable into memory. For very large variables, this load operation results inOut
of
Memory
errors. Rather than usingend
, determine the extent of a variable with thesize
method:sizeMyVar = size(matObj,'myVar')