Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Remove a structure sub-field

796 views
Skip to first unread message

Gabriele

unread,
Feb 22, 2010, 9:37:06 AM2/22/10
to
Hi all,
maybe the question is stupid, but I cannot manage to make it working.

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

Loren Shure

unread,
Feb 22, 2010, 10:56:56 AM2/22/10
to
In article <hlu4qh$7ht$1...@fred.mathworks.com>, ruga...@SPAM.libero.it
says...


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

Gabriele

unread,
Feb 22, 2010, 11:27:05 AM2/22/10
to
Loren Shure <loren...@mathworks.com> wrote in message <MPG.25ec7358f...@news.mathworks.com>...

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

Loren Shure

unread,
Feb 25, 2010, 9:38:06 AM2/25/10
to
In article <hlub8p$dee$1...@fred.mathworks.com>, ruga...@SPAM.libero.it
says...

Try this:

[newst(1:length(S)).characteristics] = S.characteristics

Ke Co

unread,
Jun 4, 2015, 12:31:25 PM6/4/15
to
"Gabriele " <ruga...@SPAM.libero.it> wrote in message <hlu4qh$7ht$1...@fred.mathworks.com>...
S.characteristics = rmfield(S.characteristics, 'x');
0 new messages