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 11:09:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 41
Message-ID: <h5maoe$ha1$1@fred.mathworks.com>
References: <a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com>
Reply-To: <HIDDEN>
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 1249816142 17729 172.30.248.35 (9 Aug 2009 11:09:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 9 Aug 2009 11:09:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1886545
Xref: news.mathworks.com comp.soft-sys.matlab:561922


David <david.b.a.epstein@googlemail.com> wrote in message <a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com>...
> 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'
>        cond: 'CANCER'
>          vf: '1'
>     protein: 'CEA_01'
> 
> 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?
> 
> Thanks
> David

All i can think of is:

% building my example
fPN = cellstr(['example1';'example2']);
ta{1} = struct('num', 1, 'cond', 'CANCER', 'vf', 1, 'protein',  'CEA_01');
ta{2} = struct('num', 1, 'cond', 'CANCER', 'vf', 1, 'protein',  'CEA_01');

% solution 1 
Temp = cellfun(@(x) struct2cell(x), ta, 'un',0);
Temp = [Temp{:}]; Temp(end+1,:) = fPN;   
ta = cell2struct(Temp, {'num', 'cond', 'vf', 'protein', 'newfield'});

in this case "ta" becomes a 2x1 struct (not a cell anymore).

Otherways u can do a simple loop:
% solution 2
for i = 1:length(ta)
    ta{i}.newfield =  fPN{i};
end