Why does 'rmfield' stop my structure from being saved?

Hi all,
I'm working with a rather large dataset - about 500 MB - which is stored in a multi-level structure. It has a few first-level fields and a deeper one. During the analysis another structure is stored within that field, so I have something to the effect of:
bat_data.img.in=matrix1;
bat_data.img.fieldmap=matrix2;
bat_data.img.reconstruction=another_structure;
Now in the next step I try replacing another_structure with a matrix (say matrix3). This is how I do it:
bat_data.img=rmfield(bat_data.img,'reconstruction');
bat_data.img.reconstruction=matrix3;
Later I also tried a simpler version (though it somehow seems less clean):
bat_data.img.reconstruction=data_reconstructed;
Both work and the structure looks and behaves as expected, until it comes to saving it:
save('everything','bat_data')
which is when I get the following message:
Warning: Variable 'bat_data' cannot be saved to a MAT-file whose version is older than 7.3.
To save this variable, use the -v7.3 switch.
Skipping...
Now using the -v7.3 switch indeed does the trick and the data gets saved, but that means every time I use this dataset (and it happens a lot) and I want to save it after further processing I need to add the switch, as will everyone else who works with this data. It's not impossible, but it's an inconvenience. Saving just before the rmfield step does not cause this issue.
The curious part is that when I try to reproduce it on some simple random set, the error doesn't occur. The code below works correctly:
%Init structures
test_struct=struct();
structure_inside=struct();
% Define the inside structure
structure_inside.infield1=rand(4);
structure_inside.infield2=rand(3);
%Define the top-level structure
test_struct.field1=rand(5);
test_struct.field2=[];
test_struct.field2.sub1=structure_inside;
test_struct.field2.sub2=rand(2);
%Delete the interior structure, replace with matrix;
test_struct.field2=rmfield(test_struct.field2,'sub1');
test_struct.field2.sub1=rand(2);
% Save, clear, reload
save('structure_test','test_struct');
clear all
load structure_test
% See if the field exists
test_struct.field2.sub1
I'm running R2014b on a 64-bit Windows 10 if that's relevant in any way.
Has anybody encountered this problem before? Why does it only happen with a large dataset (I have enough memory)? Am I just missing something obvious? Hints or answers would be greatly appreciated!
Thanks in advance,
Jedrek

Answers (1)

You might possibly be hitting the 2 gigabyte limit somehow .
There was a case very much like this one about a week ago, in which it turned out that the person was looking at the wrong variable. http://www.mathworks.com/matlabcentral/answers/307044-problem-with-save-function
Note: using rmfield requires that the bat_data.img structure be copied without the field, and then when you assign to the field the reduced bat_data.img structure has to be copied again. The work will not be done "in place". Assigning over top of the field, on the other hand, affects only the one field; the reference count for that datablock gets reduced, the old datablock gets recycled if its count goes down to 0, the reference count for the new datablock gets increased, bash the pointer to point to the new datablock -- very efficient.

Categories

Asked:

on 16 Oct 2016

Answered:

on 16 Oct 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!