Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news2.google.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe14.iad.POSTED!7564ea0f!not-for-mail
From: Doug Schwarz <see@sig.for.address.edu>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Concatenate structure fields with empties
References: <gjo71f$ct5$1@fred.mathworks.com> <see-C54CAA.13113803012009@news.frontiernet.net> <gjqtvv$ku0$1@fred.mathworks.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
User-Agent: MT-NewsWatcher/3.5.2 (Intel Mac OS X)
Message-ID: <see-262854.14274104012009@news.frontiernet.net>
Lines: 72
X-Complaints-To: abuse-news@frontiernet.net
NNTP-Posting-Date: Sun, 04 Jan 2009 19:27:38 UTC
Organization: Frontier
Date: Sun, 04 Jan 2009 14:27:41 -0500
Xref: news.mathworks.com comp.soft-sys.matlab:509768


In article <gjqtvv$ku0$1@fred.mathworks.com>,
 "Sven" <sven.holcombe@gmail.deleteme.com> wrote:

> 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.


Not by itself, but you can do it with cellfun:

  myCell = {s.myField};
  emptyIndices = cellfun(@isempty,myCell);

-- 
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.