<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239866</link>
    <title>MATLAB Central Newsreader - problem concatenating structs</title>
    <description>Feed for thread: problem concatenating 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>Mon, 24 Nov 2008 23:18:02 -0500</pubDate>
      <title>problem concatenating structs</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239866#613048</link>
      <author>Lars Barring</author>
      <description>Hi, &lt;br&gt;
I have some problems concatenating structs.&lt;br&gt;
Here is a minimal example&lt;br&gt;
&lt;br&gt;
% fill out a struct&lt;br&gt;
a.a=1;&lt;br&gt;
a.b=2;&lt;br&gt;
a.c=3;&lt;br&gt;
a(1).d=4; &lt;br&gt;
% copy this, and add another field &lt;br&gt;
b=a;&lt;br&gt;
b.e=5;&lt;br&gt;
% copy the first one (don't want to destroy it...)&lt;br&gt;
c=a;&lt;br&gt;
c(1)=b;    % no no did not work, results in:&lt;br&gt;
??? Subscripted assignment between dissimilar structures.&lt;br&gt;
&lt;br&gt;
c=b;        % did work&lt;br&gt;
% to me this is somewhat strange, &lt;br&gt;
% especially in the light that the following works&lt;br&gt;
&lt;br&gt;
a(2).f=6:  % works fine:&lt;br&gt;
&lt;br&gt;
a = &lt;br&gt;
1x2 struct array with fields:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f&lt;br&gt;
&lt;br&gt;
a(1)&lt;br&gt;
ans = &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a: 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b: 2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c: 3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d: 4&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f: []&lt;br&gt;
&lt;br&gt;
&amp;gt; a(2)&lt;br&gt;
ans = &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a: []&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b: []&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c: []&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d: []&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f: 6&lt;br&gt;
&lt;br&gt;
In reality I am using Jos' excellent FEX contribution CATSTRUCT:&lt;br&gt;
&lt;br&gt;
D=C(k);&lt;br&gt;
D.newfield=xyz;&lt;br&gt;
%etc&lt;br&gt;
C(k)=catstruct(C(k),D);   % fails&lt;br&gt;
&lt;br&gt;
CATSTRUCT works as expected but because there is an index to C the line fails.&lt;br&gt;
&lt;br&gt;
Any ideas how to solve this?&lt;br&gt;
&lt;br&gt;
Lars</description>
    </item>
    <item>
      <pubDate>Tue, 25 Nov 2008 00:52:01 -0500</pubDate>
      <title>Re: problem concatenating structs</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239866#613059</link>
      <author>Walter Roberson</author>
      <description>Lars Barring wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; I have some problems concatenating structs.&lt;br&gt;
&amp;gt; Here is a minimal example&lt;br&gt;
&lt;br&gt;
You cannot concatenate structs unless they have exactly the same set of fields&lt;br&gt;
in exactly the same order. Unless Mathworks changes this in some future version,&lt;br&gt;
you will have to find some other way of doing things.&lt;br&gt;
&lt;br&gt;
&amp;gt; In reality I am using Jos' excellent FEX contribution CATSTRUCT:&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; D=C(k);&lt;br&gt;
&amp;gt; D.newfield=xyz;&lt;br&gt;
&amp;gt; %etc&lt;br&gt;
&amp;gt; C(k)=catstruct(C(k),D);   % fails&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; CATSTRUCT works as expected but because there is an index to C the line fails.&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; Any ideas how to solve this?&lt;br&gt;
&lt;br&gt;
If you want dissimilar structures, use cell arrays. Otherwise, write a small&lt;br&gt;
routine:&lt;br&gt;
&lt;br&gt;
FN = setdiff(fieldnames(C),fieldnames(D));&lt;br&gt;
for FI = 1 : length(FN); C(1).(FN) = repmat(D.(FN),0,0); end&lt;br&gt;
&lt;br&gt;
C(k) = catstruct(C(k), D);&lt;br&gt;
&lt;br&gt;
The repmat of size 0 0 is to get the right class in place; if you know that you&lt;br&gt;
are going to be using catstruct immediately afterwards, you should probably be able&lt;br&gt;
to use [] (the empty array) instead of the repmat() call.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
.signature note: I am now avoiding replying to unclear or ambiguous postings.&lt;br&gt;
Please review questions before posting them. Be specific. Use examples of what you mean,&lt;br&gt;
of what you don't mean. Specify boundary conditions, and data classes and value&lt;br&gt;
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?</description>
    </item>
    <item>
      <pubDate>Tue, 25 Nov 2008 08:43:02 -0500</pubDate>
      <title>Re: problem concatenating structs</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239866#613114</link>
      <author>Lars Barring</author>
      <description>Walter Roberson &amp;lt;roberson@hushmail.com&amp;gt; wrote &lt;br&gt;
&lt;br&gt;
&amp;gt; Lars Barring wrote: &lt;br&gt;
8&amp;lt;&amp;lt;&amp;lt;&amp;lt;SNIP&lt;br&gt;
&amp;gt; You cannot concatenate structs unless they have exactly the same set of fields&lt;br&gt;
&amp;gt; in exactly the same order. Unless Mathworks changes this in some future version,&lt;br&gt;
&amp;gt; you will have to find some other way of doing things.&lt;br&gt;
&lt;br&gt;
Well, hopefully they do, because the minimal example points at some less than &lt;br&gt;
obvious (to me) behaviour :-)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;gt; If you want dissimilar structures, use cell arrays. Otherwise, write a small&lt;br&gt;
&amp;gt; routine:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; FN = setdiff(fieldnames(C),fieldnames(D));&lt;br&gt;
&amp;gt; for FI = 1 : length(FN); C(1).(FN) = repmat(D.(FN),0,0); end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; C(k) = catstruct(C(k), D);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The repmat of size 0 0 is to get the right class in place; if you know that you&lt;br&gt;
&amp;gt; are going to be using catstruct immediately afterwards, you should probably be &lt;br&gt;
&amp;gt; able to use [] (the empty array) instead of the repmat() call.&lt;br&gt;
&lt;br&gt;
Thanks, this is probably what I will be forced to do. But this code snippet have to&lt;br&gt;
be at many places be in the calling code rather than in a function, or in catstruct itself.&lt;br&gt;
&lt;br&gt;
Sigh. &lt;br&gt;
&lt;br&gt;
Lars</description>
    </item>
  </channel>
</rss>

