Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Remove a structure sub-field

Subject: Remove a structure sub-field

From: Gabriele

Date: 22 Feb, 2010 14:37:06

Message: 1 of 4

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

Subject: Remove a structure sub-field

From: Loren Shure

Date: 22 Feb, 2010 15:56:56

Message: 2 of 4

In article <hlu4qh$7ht$1@fred.mathworks.com>, ruga.ANTI@SPAM.libero.it
says...
> 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
>


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

Subject: Remove a structure sub-field

From: Gabriele

Date: 22 Feb, 2010 16:27:05

Message: 3 of 4

Loren Shure <loren.shure@mathworks.com> wrote in message <MPG.25ec7358fb9d7841989ab9@news.mathworks.com>...
> In article <hlu4qh$7ht$1@fred.mathworks.com>, ruga.ANTI@SPAM.libero.it
> says...
> > 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
> >
>
>
> 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

Subject: Remove a structure sub-field

From: Loren Shure

Date: 25 Feb, 2010 14:38:06

Message: 4 of 4

In article <hlub8p$dee$1@fred.mathworks.com>, ruga.ANTI@SPAM.libero.it
says...
> Loren Shure <loren.shure@mathworks.com> wrote in message <MPG.25ec7358fb9d7841989ab9@news.mathworks.com>...
> > In article <hlu4qh$7ht$1@fred.mathworks.com>, ruga.ANTI@SPAM.libero.it
> > says...
> > > 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
> > >
> >
> >
> > 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

--
Loren
http://blogs.mathworks.com/loren
http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us