Path: news.mathworks.com!not-for-mail
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: <MPG.25ec7358fb9d7841989ab9@news.mathworks.com>
References: <hlu4qh$7ht$1@fred.mathworks.com>
NNTP-Posting-Host: shurel.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-15"
Content-Transfer-Encoding: 7bit
X-Trace: fred.mathworks.com 1266854216 17247 172.31.57.214 (22 Feb 2010 15:56:56 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 22 Feb 2010 15:56:56 +0000 (UTC)
User-Agent: MicroPlanet-Gravity/2.70.2067
Xref: news.mathworks.com comp.soft-sys.matlab:610492

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