Path: news.mathworks.com!not-for-mail
From: "Sven" <sven.holcombe@gmail.deleteme.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Concatenate structure fields with empties
Date: Sun, 4 Jan 2009 18:09:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 50
Message-ID: <gjqtvv$ku0$1@fred.mathworks.com>
References: <gjo71f$ct5$1@fred.mathworks.com> <see-C54CAA.13113803012009@news.frontiernet.net>
Reply-To: "Sven" <sven.holcombe@gmail.deleteme.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1231092543 21440 172.30.248.35 (4 Jan 2009 18:09:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 4 Jan 2009 18:09:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1326470
Xref: news.mathworks.com comp.soft-sys.matlab:509766


Doug Schwarz <see@sig.for.address.edu> wrote in message <see-C54CAA.13113803012009@news.frontiernet.net>...
> In article <gjo71f$ct5$1@fred.mathworks.com>,
>  "Sven" <sven.holcombe@gmail.deleteme.com> wrote:
> 
> > Hi there, just a question about concatenating structure array fields where 
> > some of the arrays don't have that field initialised. For example:
> > 
> > s(1).myField = 100;
> > s(10).myField = 200;
> > 
> > [s.myField], or cat(1,s.myfield), both give a result of length 2 since all 
> > the other entries are empty.
> > 
> > I understand you can't have a 1 by 10 array with elements 2 to 9 being empty. 
> > How about instead returning those elements as nan?
> > 
> > Basically, what's the most efficient way for me to concatenate these 
> > structure fields, yet retain the indices where each element was found in the 
> > structure.
> > 
> > Cheers,
> > Sven.
> 
> 
> I would say try initializing your structure with NaNs and then overwrite 
> some of them with your existing code.
> 
>   s = struct('myField',num2cell(NaN(1,10)));
>   s(1).myField = 100;
>   s(10).myField = 200;
>   [s.myField]
>   ans =
>      100   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   200

Thanks Doug, I thought that might be the case. Now perhaps you can help me with a related question....
Let's say that I hadn't initialised with NaNs and wanted to somehow find the indices of the empty fields. My somewhat roundabout method is as follows:

emptyIndices = false(1,length(s));
myCell = {s.myField};
for i = 1:length(myCell)
   if isempty(myCell{i})
      emptyIndices(i) = true;
   end
end

Is there any way to turn this into a 1 or 2-liner?  Ie, can isempty() somehow take in a cell array and return whether the *contents* of each cell is empty, rather than the cell array itself being empty?

Cheers,
Sven.