Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: cell arrays and structs
Date: Sun, 9 Aug 2009 19:02:01 +0000 (UTC)
Organization: Xoran Technologies
Lines: 49
Message-ID: <h5n6f9$pm3$1@fred.mathworks.com>
References: <a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com> <see-82000A.08402409082009@news.frontiernet.net> <h5mu19$lof$1@fred.mathworks.com> <see-846576.14050809082009@news.frontiernet.net>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1249844521 26307 172.30.248.37 (9 Aug 2009 19:02:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 9 Aug 2009 19:02:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:561955


Doug Schwarz <see@sig.for.address.edu> wrote in message <see-846576.14050809082009@news.frontiernet.net>...
> 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.


Note that this solution doesn't really avoid for-loops. num2cell.m uses for loops internally...