<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Concatenating structs</title>
	<atom:link href="http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Loren Shure</title>
		<link>http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-32883</link>
		<dc:creator>Loren Shure</dc:creator>
		<pubDate>Thu, 05 Jan 2012 23:45:20 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-32883</guid>
		<description>Davis-

You are right - sorry about that!

--loren</description>
		<content:encoded><![CDATA[<p>Davis-</p>
<p>You are right &#8211; sorry about that!</p>
<p>&#8211;loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davis Bennett</title>
		<link>http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-32882</link>
		<dc:creator>Davis Bennett</dc:creator>
		<pubDate>Thu, 05 Jan 2012 23:06:22 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-32882</guid>
		<description>Loren, thanks for this guide.  I wonder, when you check for uniqueness of the fieldnames with

ufn = length(fn) == unique(length(fn))


do you mean to use


ufn = length(fn) == length(unique(fn))
</description>
		<content:encoded><![CDATA[<p>Loren, thanks for this guide.  I wonder, when you check for uniqueness of the fieldnames with</p>
<p>ufn = length(fn) == unique(length(fn))</p>
<p>do you mean to use</p>
<p>ufn = length(fn) == length(unique(fn))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Guangping Zhang</title>
		<link>http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-31221</link>
		<dc:creator>Guangping Zhang</dc:creator>
		<pubDate>Mon, 29 Mar 2010 03:02:32 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-31221</guid>
		<description>Loren, thank you, I fixed it out. 

Guangping,</description>
		<content:encoded><![CDATA[<p>Loren, thank you, I fixed it out. </p>
<p>Guangping,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-31220</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 29 Mar 2010 00:50:56 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-31220</guid>
		<description>Guangping-

This works for me:

&lt;pre class=&quot;code&quot;&gt;

%%
S.M1 = rand(1,54)
S.M2 = rand(1,91)
S.M3 = rand(1,31)
%%
fS = fieldnames(S);
fl(1)=length(S.(fS{1}));
fl(2)=length(S.(fS{2}));
fl(3)=length(S.(fS{3}));
%%
fn = fieldnames(S);
n = length(fn);
fl = zeros(n,1);
%%
for i=1:n %get each Mi’s length;
    fl(i)=length(S.(fS{i}));
end

&lt;/pre&gt;

I wonder if you have fS defined correctly since you don&#039;t show it.

--loren</description>
		<content:encoded><![CDATA[<p>Guangping-</p>
<p>This works for me:</p>
<pre class="code">

%%
S.M1 = rand(1,54)
S.M2 = rand(1,91)
S.M3 = rand(1,31)
%%
fS = fieldnames(S);
fl(1)=length(S.(fS{1}));
fl(2)=length(S.(fS{2}));
fl(3)=length(S.(fS{3}));
%%
fn = fieldnames(S);
n = length(fn);
fl = zeros(n,1);
%%
for i=1:n %get each Mi’s length;
    fl(i)=length(S.(fS{i}));
end
</pre>
<p>I wonder if you have fS defined correctly since you don&#8217;t show it.</p>
<p>&#8211;loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Guangping Zhang</title>
		<link>http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-31218</link>
		<dc:creator>Guangping Zhang</dc:creator>
		<pubDate>Sun, 28 Mar 2010 23:29:34 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-31218</guid>
		<description>I put this post in your another blog, but I find this blog fit my question better. So I put it again.

1X1 structure S contain M1, M2, M3....M30 which has various length, such as 54, 32, 61...42.
I write code to get the length of each M. 
If I write 
    fl(1)=length(S.(fS{1}));
    fl(2)=length(S.(fS{2}));
    fl(3)=length(S.(fS{3}));
    ..........
I will get correct length.
But if I using for loop below, answer is wrong, all equal 0.

fn=fieldnames(S); % all the field name was put into cell fn;
n=length(fn); % get fn length;
for i=1:n %get each Mi&#039;s length;
    fl(i)=length(S.(fS{i}));
end

Dear Loren, could you please help me? Why the loop don&#039;t work?</description>
		<content:encoded><![CDATA[<p>I put this post in your another blog, but I find this blog fit my question better. So I put it again.</p>
<p>1X1 structure S contain M1, M2, M3&#8230;.M30 which has various length, such as 54, 32, 61&#8230;42.<br />
I write code to get the length of each M.<br />
If I write<br />
    fl(1)=length(S.(fS{1}));<br />
    fl(2)=length(S.(fS{2}));<br />
    fl(3)=length(S.(fS{3}));<br />
    &#8230;&#8230;&#8230;.<br />
I will get correct length.<br />
But if I using for loop below, answer is wrong, all equal 0.</p>
<p>fn=fieldnames(S); % all the field name was put into cell fn;<br />
n=length(fn); % get fn length;<br />
for i=1:n %get each Mi&#8217;s length;<br />
    fl(i)=length(S.(fS{i}));<br />
end</p>
<p>Dear Loren, could you please help me? Why the loop don&#8217;t work?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neil</title>
		<link>http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-30916</link>
		<dc:creator>Neil</dc:creator>
		<pubDate>Sat, 26 Dec 2009 23:04:04 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-30916</guid>
		<description>Loren, Helpful, yes. I use structures to hold configuration data, with some fields ignored, all the time. Liked your posts on concatenating structs &amp; using empty arrays to trigger use of defaults. I went a different way. I tend to use structs of configuration parameters. In my cases, I want the provided fields to replace the default fields &amp; the default ones to persist if not replaced. Functions know what to look for, if extra fields are there, simple function don&#039;t look for them. More complicated function look for additional fields, eg nested configuration structures to pass on. Hence wrote a structureUnionRecursive. But, I have some issues. I&#039;ll try to post on the forum &amp; see if I&#039;m being silly, or any others have some of my issues.</description>
		<content:encoded><![CDATA[<p>Loren, Helpful, yes. I use structures to hold configuration data, with some fields ignored, all the time. Liked your posts on concatenating structs &amp; using empty arrays to trigger use of defaults. I went a different way. I tend to use structs of configuration parameters. In my cases, I want the provided fields to replace the default fields &amp; the default ones to persist if not replaced. Functions know what to look for, if extra fields are there, simple function don&#8217;t look for them. More complicated function look for additional fields, eg nested configuration structures to pass on. Hence wrote a structureUnionRecursive. But, I have some issues. I&#8217;ll try to post on the forum &amp; see if I&#8217;m being silly, or any others have some of my issues.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-30759</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 10 Nov 2009 19:05:54 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-30759</guid>
		<description>Hello A Different Ben!

There is nothing ready to go for you, but there are some tools that should make your life easier.  ismember is the main one that occurs to me now.  

Here I sketch my thought process in how to do this.  It might not be the most efficient, but it has all the right steps, I think.

Either know which fields are in common, e.g., .name, or write a little code to find the overlaps. For the overlap ones, you need to get the values for both the first and second structs, and you can then use ismember with multiple outputs to figure out which locations in struct2 match the list in struct1.  You can then reorder the struct2 values according to the ismember match indices.  You can then delete the matching field (e.g., name) from struct.  And finally, merge struct2 into struct1.

Good luck!

--Loren</description>
		<content:encoded><![CDATA[<p>Hello A Different Ben!</p>
<p>There is nothing ready to go for you, but there are some tools that should make your life easier.  ismember is the main one that occurs to me now.  </p>
<p>Here I sketch my thought process in how to do this.  It might not be the most efficient, but it has all the right steps, I think.</p>
<p>Either know which fields are in common, e.g., .name, or write a little code to find the overlaps. For the overlap ones, you need to get the values for both the first and second structs, and you can then use ismember with multiple outputs to figure out which locations in struct2 match the list in struct1.  You can then reorder the struct2 values according to the ismember match indices.  You can then delete the matching field (e.g., name) from struct.  And finally, merge struct2 into struct1.</p>
<p>Good luck!</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: A different Ben</title>
		<link>http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-30758</link>
		<dc:creator>A different Ben</dc:creator>
		<pubDate>Tue, 10 Nov 2009 16:49:29 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-30758</guid>
		<description>Hi Loren,

I&#039;ve got a slight variation on the merger you&#039;ve presented - I want to use a struct like a linked list for merger. 

In your example, the heights in s2 are in the same order as the entries of s1. In my problem, I may not know the order of the second structure, and want to use one value as an index to add to the first structure. That is, given a structure that is like this:

&lt;pre&gt;
s1.name = &#039;fred&#039;;
s1.age = 42;
s1(2).name = &#039;alice&#039;;
s1(2).age = 29;
&lt;/pre&gt;

and a second structure like this:

&lt;pre&gt;
s2.name = &#039;alice&#039;;
s2.height = 160;
s2(2).name = &#039;fred&#039;;
s2(2).height = 170;
&lt;/pre&gt;

how can I use the name to find the appropriate height to add from s2 to the correct substructure of s1? I &#039;d like to end up with a structure like this:

&lt;pre&gt;
s1.name = &#039;fred&#039;;
s1.age = 42;
s1.height = 170;
s1(2).name = &#039;alice&#039;;
s1(2).age = 29;
s1(2).height = 160;
&lt;/pre&gt;

Is there an easy way to do such a thing?</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>I&#8217;ve got a slight variation on the merger you&#8217;ve presented &#8211; I want to use a struct like a linked list for merger. </p>
<p>In your example, the heights in s2 are in the same order as the entries of s1. In my problem, I may not know the order of the second structure, and want to use one value as an index to add to the first structure. That is, given a structure that is like this:</p>
<pre>
s1.name = 'fred';
s1.age = 42;
s1(2).name = 'alice';
s1(2).age = 29;
</pre>
<p>and a second structure like this:</p>
<pre>
s2.name = 'alice';
s2.height = 160;
s2(2).name = 'fred';
s2(2).height = 170;
</pre>
<p>how can I use the name to find the appropriate height to add from s2 to the correct substructure of s1? I &#8216;d like to end up with a structure like this:</p>
<pre>
s1.name = 'fred';
s1.age = 42;
s1.height = 170;
s1(2).name = 'alice';
s1(2).age = 29;
s1(2).height = 160;
</pre>
<p>Is there an easy way to do such a thing?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pete Scotson</title>
		<link>http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-30680</link>
		<dc:creator>Pete Scotson</dc:creator>
		<pubDate>Fri, 16 Oct 2009 11:50:30 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-30680</guid>
		<description>Loren,

Yes, I do need to merge structures. I have tried and customised the functions on file exchange but I usually want to merge structures in a very specific way with custom error handling etc.

Mostly I use structures as &quot;contents addressable&quot; containers for data. Such a format makes finding the record easy using dynamic field names in code, and works well for keyboard work (with tab completion).

&lt;pre&gt;
tmp.age       = 42;
tmp.height    = 170;
mystruc.fred  = tmp;

tmp.age       = 29;
tmp.height    = 160;
mystruc.alice = tmp;

...

name=&#039;alice&#039;;
mystruc.(name).age
&lt;/pre&gt;

I rarely want to do statistical analysis across the records because I usually want to do computations on the data within the individual record. Structfun can be used to get the items such as the ages into an array.

Regards,
Pete</description>
		<content:encoded><![CDATA[<p>Loren,</p>
<p>Yes, I do need to merge structures. I have tried and customised the functions on file exchange but I usually want to merge structures in a very specific way with custom error handling etc.</p>
<p>Mostly I use structures as &#8220;contents addressable&#8221; containers for data. Such a format makes finding the record easy using dynamic field names in code, and works well for keyboard work (with tab completion).</p>
<pre>
tmp.age       = 42;
tmp.height    = 170;
mystruc.fred  = tmp;

tmp.age       = 29;
tmp.height    = 160;
mystruc.alice = tmp;

...

name='alice';
mystruc.(name).age
</pre>
<p>I rarely want to do statistical analysis across the records because I usually want to do computations on the data within the individual record. Structfun can be used to get the items such as the ages into an array.</p>
<p>Regards,<br />
Pete</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-30679</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 16 Oct 2009 11:29:18 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/#comment-30679</guid>
		<description>Ben-

I recommend you use the link on the right to make the suggestion to Technical Support. 

--Loren</description>
		<content:encoded><![CDATA[<p>Ben-</p>
<p>I recommend you use the link on the right to make the suggestion to Technical Support. </p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
</channel>
</rss>

