Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news1.google.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe19.iad.POSTED!7564ea0f!not-for-mail
From: Doug Schwarz <see@sig.for.address.edu>
Newsgroups: comp.soft-sys.matlab
Subject: Re: cell arrays and structs
References: <a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com> <see-82000A.08402409082009@news.frontiernet.net> <h5mu19$lof$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-846576.14050809082009@news.frontiernet.net>
Lines: 50
X-Complaints-To: abuse-news@frontiernet.net
NNTP-Posting-Date: Sun, 09 Aug 2009 18:05:09 UTC
Organization: Frontier
Date: Sun, 09 Aug 2009 14:05:09 -0400
Xref: news.mathworks.com comp.soft-sys.matlab:561951


In article <h5mu19$lof$1@fred.mathworks.com>,
 "us " <us@neurol.unizh.ch> wrote:

> Doug Schwarz <see@sig.for.address.edu> wrote in message 
> <see-82000A.08402409082009@news.frontiernet.net>...
> > In article 
> > <a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com>,
> >  David <david.b.a.epstein@googlemail.com> wrote:
> > > I have a 380x1 cell array fPN. Each cell has class 'char' and contains
> > > a string.
> > > I also have a 380x1 cell array ta. Each cell has class 'struct'. For
> > > example I get
> > > >> ta{5}
> > > ans =
> > >         num: '1'
> > > My question is: Is there an array method to add to each ta{i} a new
> > > field whose value will be the string in fPN{i}, or should I just give
> > > up and loop through the cells, adding the new field one by one?
> 
> > Yes, suppose you want to use the field name 'string', then
> >   [ta.string] = fPN{:};
> > will do it if your version of MATLAB is new enough.
> 
> no - does not work...
> according to the OP's spec, his/her TAs are CELLs of STRUCTs - and not just 
> STRUCTs...
> hence, a simple for loop is probably the fastest solution...
> 
> us

You're right, Urs, I missed that.  If each cell in ta is a struct with 
the same fields they could be concatenated into a proper struct array 
with

  ta2 = [ta{:}];

and then

  [ta2.string] = fPN{:};

would work.  Then ta2 could be converted back to a cell array with

  c2 = num2cell(ta2);

if desired.  But really it would be better to use ta2.

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