Hi all,
I'm trying to save some variables to a .mat file, appending to that file if the variable is new, overwriting if it's already there. I've found different behaviour if the variable is an object vs if it's a simple array. Here's a demonstration:
Firstly, arrays
p = rand(1000,3);save('test.mat','p')whos '-file' test.matd1 = dir('test.mat');p = rand(10,3);save('test.mat','p','-append')whos '-file' test.matd2 = dir('test.mat');fprintf('Saved .mat file:\n1000 pt triangulation: %d bytes\n 10 pt triangulation: %d bytes.\n',... d1.bytes, d2.bytes)
Results in:
Saved .mat file:1000 pt triangulation: 22907 bytes 10 pt triangulation: 430 bytes.
Now, objects
p = delaunayTriangulation(rand(1000,3));save('test.mat','p')whos '-file' test.matd1 = dir('test.mat');p = delaunayTriangulation(rand(10,3));save('test.mat','p','-append')whos '-file' test.matd2 = dir('test.mat');fprintf('Saved .mat file:\n1000 pt triangulation: %d bytes\n 10 pt triangulation: %d bytes.\n',... d1.bytes, d2.bytes)
Results in:
Saved .mat file:1000 pt triangulation: 72833 bytes 10 pt triangulation: 73319 bytes.
As you can see, the first -append with arrays did exactly as expected. The large variable p was overwritten by the smaller variable with the same name. The resulting filesize was reduced (as is expected).
The second -append worked differently. Here, it seems that the original large p object stayed in the file, even after it was replaced with a much smaller object with the same name. Overall, the filesize actually increased. This is surprising. Is this a bug? A feature with dubious utility? It's an annoyance at the moment. The only way I've found so far is to reload the whole contents of a .mat file into memory every time I want to -append a replacement to ANY of the object variables in that file. This would be fine with small files, but I'm dealing with a few hundred MB files containing a suite of 100 MB objects – it's really cumbersome to reload all when I only need to save one.
Thanks, Sven.
Best Answer