How to create variable in MAT file that is too large to fit in memory?
7 views (last 30 days)
Show older comments
The new 2011b release provides the matfile object, allowing you to load a portion of a large MAT-file variable using indices. This is useful if the entire variable cannot fit in memory.
But my question is, how can I create such a large variable in a MAT file in the first place, if I cannot construct it entirely in memory first? The save -append option only adds variables to a file; it does not append elements to a variable in the file.
I see that the matfile object allows for partial save (by indices) in the same manner as partial read, so if I could somehow allocate such a large variable (like a zeros() for matfile), then I could write to it piecemeal.
Accepted Answer
Jeff
on 18 May 2012
1 Comment
NbrOneProcastinator
on 6 Jun 2023
Although this thread is already old I have to comment on this answer, which is actually not quite right. To preallocate memory for a large matfile you have to define the last element, e.g.
matObj.myVar(1000,1000) = 0;
Notice the comma instead of the colon.
More Answers (2)
James Tursa
on 7 Sep 2011
You could create the MAT file from scratch in a Fortran or C/C++ program using the published uncompressed MAT file format. E.g., here:
A variable of the desired size doesn't have to exist in memory in order for the MAT file to be written.
the cyclist
on 7 Sep 2011
One simple possibility is that the large array was created and saved when it was the only variable in the workspace, but later is loaded when the workspace is littered with other variables, so there is only room for part of it.
2 Comments
Walter Roberson
on 7 Sep 2011
Or the large array was constructed on a system which had more memory, including possibly a 64 bit MATLAB version for a file being loaded on a 32 bit MATLAB version.
Subnote: if you are planning to use such large matrices, you should probably be saving with -v7.3 as your files might be too large for -v7 . But beware that -v7.3 file performance can be much worse than -v7 file performance (unless they fixed that.)
See Also
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!