<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258090</link>
    <title>MATLAB Central Newsreader - cell arrays and structs</title>
    <description>Feed for thread: cell arrays and structs</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sun, 09 Aug 2009 10:38:58 -0400</pubDate>
      <title>cell arrays and structs</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258090#671732</link>
      <author>David</author>
      <description>I have a 380x1 cell array fPN. Each cell has class 'char' and contains&lt;br&gt;
a string.&lt;br&gt;
I also have a 380x1 cell array ta. Each cell has class 'struct'. For&lt;br&gt;
example I get&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; ta{5}&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;num: '1'&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cond: 'CANCER'&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vf: '1'&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;protein: 'CEA_01'&lt;br&gt;
&lt;br&gt;
My question is: Is there an array method to add to each ta{i} a new&lt;br&gt;
field whose value will be the string in fPN{i}, or should I just give&lt;br&gt;
up and loop through the cells, adding the new field one by one?&lt;br&gt;
&lt;br&gt;
Thanks&lt;br&gt;
David</description>
    </item>
    <item>
      <pubDate>Sun, 09 Aug 2009 11:09:02 -0400</pubDate>
      <title>Re: cell arrays and structs</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258090#671734</link>
      <author>Oleg Komarov</author>
      <description>David &amp;lt;david.b.a.epstein@googlemail.com&amp;gt; wrote in message &amp;lt;a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; I have a 380x1 cell array fPN. Each cell has class 'char' and contains&lt;br&gt;
&amp;gt; a string.&lt;br&gt;
&amp;gt; I also have a 380x1 cell array ta. Each cell has class 'struct'. For&lt;br&gt;
&amp;gt; example I get&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; ta{5}&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;         num: '1'&lt;br&gt;
&amp;gt;        cond: 'CANCER'&lt;br&gt;
&amp;gt;          vf: '1'&lt;br&gt;
&amp;gt;     protein: 'CEA_01'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My question is: Is there an array method to add to each ta{i} a new&lt;br&gt;
&amp;gt; field whose value will be the string in fPN{i}, or should I just give&lt;br&gt;
&amp;gt; up and loop through the cells, adding the new field one by one?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks&lt;br&gt;
&amp;gt; David&lt;br&gt;
&lt;br&gt;
All i can think of is:&lt;br&gt;
&lt;br&gt;
% building my example&lt;br&gt;
fPN = cellstr(['example1';'example2']);&lt;br&gt;
ta{1} = struct('num', 1, 'cond', 'CANCER', 'vf', 1, 'protein',  'CEA_01');&lt;br&gt;
ta{2} = struct('num', 1, 'cond', 'CANCER', 'vf', 1, 'protein',  'CEA_01');&lt;br&gt;
&lt;br&gt;
% solution 1 &lt;br&gt;
Temp = cellfun(@(x) struct2cell(x), ta, 'un',0);&lt;br&gt;
Temp = [Temp{:}]; Temp(end+1,:) = fPN;   &lt;br&gt;
ta = cell2struct(Temp, {'num', 'cond', 'vf', 'protein', 'newfield'});&lt;br&gt;
&lt;br&gt;
in this case &quot;ta&quot; becomes a 2x1 struct (not a cell anymore).&lt;br&gt;
&lt;br&gt;
Otherways u can do a simple loop:&lt;br&gt;
% solution 2&lt;br&gt;
for i = 1:length(ta)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ta{i}.newfield =  fPN{i};&lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Sun, 09 Aug 2009 12:40:24 -0400</pubDate>
      <title>Re: cell arrays and structs</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258090#671741</link>
      <author>Doug Schwarz</author>
      <description>In article &lt;br&gt;
&amp;lt;a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com&amp;gt;,&lt;br&gt;
&amp;nbsp;David &amp;lt;david.b.a.epstein@googlemail.com&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; I have a 380x1 cell array fPN. Each cell has class 'char' and contains&lt;br&gt;
&amp;gt; a string.&lt;br&gt;
&amp;gt; I also have a 380x1 cell array ta. Each cell has class 'struct'. For&lt;br&gt;
&amp;gt; example I get&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; ta{5}&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;         num: '1'&lt;br&gt;
&amp;gt;        cond: 'CANCER'&lt;br&gt;
&amp;gt;          vf: '1'&lt;br&gt;
&amp;gt;     protein: 'CEA_01'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My question is: Is there an array method to add to each ta{i} a new&lt;br&gt;
&amp;gt; field whose value will be the string in fPN{i}, or should I just give&lt;br&gt;
&amp;gt; up and loop through the cells, adding the new field one by one?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks&lt;br&gt;
&amp;gt; David&lt;br&gt;
&lt;br&gt;
Yes, suppose you want to use the field name 'string', then&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;[ta.string] = fPN{:};&lt;br&gt;
&lt;br&gt;
will do it if your version of MATLAB is new enough.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Doug Schwarz&lt;br&gt;
dmschwarz&amp;ieee,org&lt;br&gt;
Make obvious changes to get real email address.</description>
    </item>
    <item>
      <pubDate>Sun, 09 Aug 2009 15:20:02 -0400</pubDate>
      <title>Re: cell arrays and structs</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258090#671753</link>
      <author>Matt </author>
      <description>Doug Schwarz &amp;lt;see@sig.for.address.edu&amp;gt; wrote in message &amp;lt;see-82000A.08402409082009@news.frontiernet.net&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; Yes, suppose you want to use the field name 'string', then&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   [ta.string] = fPN{:};&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; will do it if your version of MATLAB is new enough.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Of if not, then the following should work for older versions&lt;br&gt;
&lt;br&gt;
[ta(1:length(fpN)).string] = deal(fPN{:});</description>
    </item>
    <item>
      <pubDate>Sun, 09 Aug 2009 16:38:01 -0400</pubDate>
      <title>Re: cell arrays and structs</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258090#671758</link>
      <author>us</author>
      <description>Doug Schwarz &amp;lt;see@sig.for.address.edu&amp;gt; wrote in message &amp;lt;see-82000A.08402409082009@news.frontiernet.net&amp;gt;...&lt;br&gt;
&amp;gt; In article &lt;br&gt;
&amp;gt; &amp;lt;a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com&amp;gt;,&lt;br&gt;
&amp;gt;  David &amp;lt;david.b.a.epstein@googlemail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt; I have a 380x1 cell array fPN. Each cell has class 'char' and contains&lt;br&gt;
&amp;gt; &amp;gt; a string.&lt;br&gt;
&amp;gt; &amp;gt; I also have a 380x1 cell array ta. Each cell has class 'struct'. For&lt;br&gt;
&amp;gt; &amp;gt; example I get&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;&amp;gt; ta{5}&lt;br&gt;
&amp;gt; &amp;gt; ans =&lt;br&gt;
&amp;gt; &amp;gt;         num: '1'&lt;br&gt;
&amp;gt; &amp;gt; My question is: Is there an array method to add to each ta{i} a new&lt;br&gt;
&amp;gt; &amp;gt; field whose value will be the string in fPN{i}, or should I just give&lt;br&gt;
&amp;gt; &amp;gt; up and loop through the cells, adding the new field one by one?&lt;br&gt;
&lt;br&gt;
&amp;gt; Yes, suppose you want to use the field name 'string', then&lt;br&gt;
&amp;gt;   [ta.string] = fPN{:};&lt;br&gt;
&amp;gt; will do it if your version of MATLAB is new enough.&lt;br&gt;
&lt;br&gt;
no - does not work...&lt;br&gt;
according to the OP's spec, his/her TAs are CELLs of STRUCTs - and not just STRUCTs...&lt;br&gt;
hence, a simple for loop is probably the fastest solution...&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
    <item>
      <pubDate>Sun, 09 Aug 2009 17:02:01 -0400</pubDate>
      <title>Re: cell arrays and structs</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258090#671760</link>
      <author>Matt </author>
      <description>David &amp;lt;david.b.a.epstein@googlemail.com&amp;gt; wrote in message &amp;lt;a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; I also have a 380x1 cell array ta. Each cell has class 'struct'. For&lt;br&gt;
&amp;gt; example I get&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; ta{5}&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;         num: '1'&lt;br&gt;
&amp;gt;        cond: 'CANCER'&lt;br&gt;
&amp;gt;          vf: '1'&lt;br&gt;
&amp;gt;     protein: 'CEA_01'&lt;br&gt;
&lt;br&gt;
This seems like a bad way to store data. Why not use a struct array &lt;br&gt;
&lt;br&gt;
sta=[ta{:}];&lt;br&gt;
&lt;br&gt;
instead of burying each struct inside a cell where it is harder to get at?</description>
    </item>
    <item>
      <pubDate>Sun, 09 Aug 2009 18:05:09 -0400</pubDate>
      <title>Re: cell arrays and structs</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258090#671763</link>
      <author>Doug Schwarz</author>
      <description>In article &amp;lt;h5mu19$lof$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
&amp;nbsp;&quot;us &quot; &amp;lt;us@neurol.unizh.ch&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; Doug Schwarz &amp;lt;see@sig.for.address.edu&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;lt;see-82000A.08402409082009@news.frontiernet.net&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; In article &lt;br&gt;
&amp;gt; &amp;gt; &amp;lt;a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com&amp;gt;,&lt;br&gt;
&amp;gt; &amp;gt;  David &amp;lt;david.b.a.epstein@googlemail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; I have a 380x1 cell array fPN. Each cell has class 'char' and contains&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; a string.&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; I also have a 380x1 cell array ta. Each cell has class 'struct'. For&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; example I get&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt;&amp;gt; ta{5}&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; ans =&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;         num: '1'&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; My question is: Is there an array method to add to each ta{i} a new&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; field whose value will be the string in fPN{i}, or should I just give&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; up and loop through the cells, adding the new field one by one?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Yes, suppose you want to use the field name 'string', then&lt;br&gt;
&amp;gt; &amp;gt;   [ta.string] = fPN{:};&lt;br&gt;
&amp;gt; &amp;gt; will do it if your version of MATLAB is new enough.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; no - does not work...&lt;br&gt;
&amp;gt; according to the OP's spec, his/her TAs are CELLs of STRUCTs - and not just &lt;br&gt;
&amp;gt; STRUCTs...&lt;br&gt;
&amp;gt; hence, a simple for loop is probably the fastest solution...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; us&lt;br&gt;
&lt;br&gt;
You're right, Urs, I missed that.  If each cell in ta is a struct with &lt;br&gt;
the same fields they could be concatenated into a proper struct array &lt;br&gt;
with&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;ta2 = [ta{:}];&lt;br&gt;
&lt;br&gt;
and then&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;[ta2.string] = fPN{:};&lt;br&gt;
&lt;br&gt;
would work.  Then ta2 could be converted back to a cell array with&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;c2 = num2cell(ta2);&lt;br&gt;
&lt;br&gt;
if desired.  But really it would be better to use ta2.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Doug Schwarz&lt;br&gt;
dmschwarz&amp;ieee,org&lt;br&gt;
Make obvious changes to get real email address.</description>
    </item>
    <item>
      <pubDate>Sun, 09 Aug 2009 19:02:01 -0400</pubDate>
      <title>Re: cell arrays and structs</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258090#671767</link>
      <author>Matt </author>
      <description>Doug Schwarz &amp;lt;see@sig.for.address.edu&amp;gt; wrote in message &amp;lt;see-846576.14050809082009@news.frontiernet.net&amp;gt;...&lt;br&gt;
&amp;gt; In article &amp;lt;h5mu19$lof$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
&amp;gt;  &quot;us &quot; &amp;lt;us@neurol.unizh.ch&amp;gt; wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Doug Schwarz &amp;lt;see@sig.for.address.edu&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;gt; &amp;lt;see-82000A.08402409082009@news.frontiernet.net&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; In article &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;lt;a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com&amp;gt;,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;  David &amp;lt;david.b.a.epstein@googlemail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; I have a 380x1 cell array fPN. Each cell has class 'char' and contains&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; a string.&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; I also have a 380x1 cell array ta. Each cell has class 'struct'. For&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; example I get&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; &amp;gt;&amp;gt; ta{5}&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; ans =&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt;         num: '1'&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; My question is: Is there an array method to add to each ta{i} a new&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; field whose value will be the string in fPN{i}, or should I just give&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; up and loop through the cells, adding the new field one by one?&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Yes, suppose you want to use the field name 'string', then&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;   [ta.string] = fPN{:};&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; will do it if your version of MATLAB is new enough.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; no - does not work...&lt;br&gt;
&amp;gt; &amp;gt; according to the OP's spec, his/her TAs are CELLs of STRUCTs - and not just &lt;br&gt;
&amp;gt; &amp;gt; STRUCTs...&lt;br&gt;
&amp;gt; &amp;gt; hence, a simple for loop is probably the fastest solution...&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; us&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You're right, Urs, I missed that.  If each cell in ta is a struct with &lt;br&gt;
&amp;gt; the same fields they could be concatenated into a proper struct array &lt;br&gt;
&amp;gt; with&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   ta2 = [ta{:}];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and then&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   [ta2.string] = fPN{:};&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; would work.  Then ta2 could be converted back to a cell array with&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   c2 = num2cell(ta2);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; if desired.  But really it would be better to use ta2.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Note that this solution doesn't really avoid for-loops. num2cell.m uses for loops internally...</description>
    </item>
    <item>
      <pubDate>Sun, 09 Aug 2009 20:29:56 -0400</pubDate>
      <title>Re: cell arrays and structs</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/258090#671781</link>
      <author>David</author>
      <description>On 9 Aug, 18:02, &quot;Matt &quot; &amp;lt;x...@whatever.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; This seems like a bad way to store data. Why not use a struct array&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; sta=[ta{:}];&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; instead of burying each struct inside a cell where it is harder to get at?&lt;br&gt;
&lt;br&gt;
The reason I have a cell array is that this is what an application of&lt;br&gt;
regexp gave me. I was trying to find my way out of the cell array, but&lt;br&gt;
didn't know how. Thanks everyone for showing me the construction---&lt;br&gt;
this solves my problem.&lt;br&gt;
David</description>
    </item>
  </channel>
</rss>

