From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Remove a structure sub-field
Date: Mon, 22 Feb 2010 10:56:56 -0500
Organization: The MathWorks
Lines: 56
Message-ID: <>
References: <hlu4qh$7ht$>
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-15"
Content-Transfer-Encoding: 7bit
X-Trace: 1266854216 17247 (22 Feb 2010 15:56:56 GMT)
NNTP-Posting-Date: Mon, 22 Feb 2010 15:56:56 +0000 (UTC)
User-Agent: MicroPlanet-Gravity/2.70.2067
Xref: comp.soft-sys.matlab:610492

In article <hlu4qh$7ht$>, 
> 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".