I have a structure S, with a field ".type" and with a field ".characteristics". The field ".characteristics" has sub-fields, as, e.g., .x, .y, .z, and so on.
Therefore, for e.g. the n-th element in the structure, I can obtain the type as
S(n).type
or the x,y,z values as
S(n).characteristics.x
S(n).characteristics.y
S(n).characteristics.z
with n=1:Ntot, being Ntot the size of the structure S.
Now, I would like to remove, for example, the sub-field ".x" completely (for sake of memory saving).
I tried with rmfield, but it is not working.
I can easily do
S=rmfield(S,'type')
for removing the filed ".type"
or
S=rmfield(S,'characteristics')
to remove ".characterostics" completely with all its sub-fields. But I would like to do something like
S=rmfield(S,'characteristics.x')
which, instead, is not working...
What to do?
Thanks,
Gab
characteristics.x isn't a field which is why that doesn't work. I
recommend making a temporary struct with characteristics as the root.
newstruct = S.characteristics;
remove the x field from this. And then reset the characteristics field
in S to the newstruct. FWIW, you will NOT be making copies of the
actual data values when you do this, but you will be making temporary
structs with the right "shape".
--
Loren
http://blogs.mathworks.com/loren
http://matlabwiki.mathworks.com/MATLAB_FAQ
Hi Loren,
thank you for the fast reply.
However it seems to me your solution does not work as I would like things to work.
In particular if I use
newstruct = S.characteristics;
what I find in newstruct is only S(1).characteristics.
Therefore, what I should do to go along this line is a for loop like
for n=1:length(S),
S(n).characteristics=rmfield(S(n).characteristics,'x');
end;
Is there any way to avoid loops?
Thanks,
Gab
Try this:
[newst(1:length(S)).characteristics] = S.characteristics