<p>I want to combine two structures with differing fields names.</p>

<p>For example, starting with:</p>

<pre><code>A.field1 = 1;
A.field2 = 'a';

B.field3 = 2;
B.field4 = 'b';
</code></pre>

<p>I would like to have:</p>

<pre><code>C.field1 = 1;
C.field2 = 'a';
C.field3 = 2;
C.field4 = 'b';
</code></pre>

<p>Is there a more efficient way than using "fieldnames" and a for loop?</p>

<p><strong>EDIT:</strong> Let's assume that in the case of field name conflicts we give preference to <code>A</code>.</p>

[{'answer_id': 38659, 'author': 'pbh101', 'author_id': 1266, 'author_profile': 'https://Stackoverflow.com/users/1266', 'pm_score': 2, 'selected': False, 'text': "<p>In C, a struct can have another struct as one of it's members.  While this isn't exactly the same as what you're asking, you could end up either with a situation where one struct contains another, or one struct contains two structs, both of which hold parts of the info that you wanted.</p>\n\n<p>psuedocode: i don't remember the actual syntax.</p>\n\n<pre><code>A.field1 = 1;\nA.field2 = 'a';\nA.field3 = struct B;\n</code></pre>\n\n<p>to access:\n    A.field3.field4;</p>\n\n<p>or something of the sort.</p>\n\n<p>Or you could have struct C hold both an A and a B:</p>\n\n<pre><code>C.A = struct A;\nC.B = struct B;\n</code></pre>\n\n<p>with access then something like</p>\n\n<pre><code>C.A.field1;\nC.A.field2;\nC.B.field3;\nC.B.field4;\n</code></pre>\n\n<p>hope this helps!</p>\n\n<p>EDIT: both of these solutions avoid naming collisions.</p>\n\n<p>Also, I didn't see your <code>matlab</code> tag.  By convention, you should want to edit the question to include that piece of info.</p>\n"}, {'answer_id': 39678, 'author': 'SCFrench', 'author_id': 4928, 'author_profile': 'https://Stackoverflow.com/users/4928', 'pm_score': 5, 'selected': True, 'text': "<p>Without collisions, you can do </p>\n\n<pre><code>M = [fieldnames(A)' fieldnames(B)'; struct2cell(A)' struct2cell(B)'];\nC=struct(M{:});\n</code></pre>\n\n<p>And this is reasonably efficient. However, <code>struct</code> errors on duplicate fieldnames, and pre-checking for them using <code>unique</code> kills performance to the point that a loop is better. But here's what it would look like:</p>\n\n<pre><code>M = [fieldnames(A)' fieldnames(B)'; struct2cell(A)' struct2cell(B)'];\n\n[tmp, rows] = unique(M(1,:), 'last');\nM=M(:, rows);\n\nC=struct(M{:});\n</code></pre>\n\n<p>You might be able to make a hybrid solution by assuming no conflicts and using a try/catch around the call to <code>struct</code> to gracefully degrade to the conflict handling case.</p>\n"}, {'answer_id': 357381, 'author': 'Jason S', 'author_id': 44330, 'author_profile': 'https://Stackoverflow.com/users/44330', 'pm_score': 2, 'selected': False, 'text': '<p>I don\'t think you can handle conflicts well w/o a loop, nor do I think you\'d need to avoid one. (although I suppose efficiency could be an issue w/ many many fields...)</p>\n\n<p>I use a function I wrote a few years back called <code>setdefaults.m</code>, which combines one structure with the values of another structure, where one takes precedence over the other in case of conflict.</p>\n\n<pre><code>% SETDEFAULTS sets the default structure values \n%    SOUT = SETDEFAULTS(S, SDEF) reproduces in S \n%    all the structure fields, and their values,  that exist in \n%    SDEF that do not exist in S. \n%    SOUT = SETDEFAULTS(S, SDEF, OVERRIDE) does\n%    the same function as above, but if OVERRIDE is 1,\n%    it copies all fields of SDEF to SOUT.\n\nfunction sout = setdefaults(s,sdef,override)\nif (not(exist(\'override\',\'var\')))\n    override = 0;\nend\n\nsout = s;\nfor f = fieldnames(sdef)\'\n    cf = char(f);\n    if (override | not(isfield(sout,cf)))\n        sout = setfield(sout,cf,getfield(sdef,cf));\n    end\nend\n</code></pre>\n\n<p>Now that I think about it, I\'m pretty sure that the "override" input is unnecessary (you can just switch the order of the inputs) though I\'m not 100% sure of that... so here\'s a simpler rewrite (<code>setdefaults2.m</code>):</p>\n\n<pre><code>% SETDEFAULTS2 sets the default structure values \n%    SOUT = SETDEFAULTS(S, SDEF) reproduces in S \n%    all the structure fields, and their values,  that exist in \n%    SDEF that do not exist in S. \n\nfunction sout = setdefaults2(s,sdef)\nsout = sdef;\nfor f = fieldnames(s)\'\n    sout = setfield(sout,f{1},getfield(s,f{1}));\nend\n</code></pre>\n\n<p>and some samples to test it:</p>\n\n<pre><code>&gt;&gt; S1 = struct(\'a\',1,\'b\',2,\'c\',3);\n&gt;&gt; S2 = struct(\'b\',4,\'c\',5,\'d\',6);\n&gt;&gt; setdefaults2(S1,S2)\n\nans = \n\n    b: 2\n    c: 3\n    d: 6\n    a: 1\n\n&gt;&gt; setdefaults2(S2,S1)\n\nans = \n\n    a: 1\n    b: 4\n    c: 5\n    d: 6\n</code></pre>\n'}, {'answer_id': 18893739, 'author': 'Dennis Jaheruddin', 'author_id': 983722, 'author_profile': 'https://Stackoverflow.com/users/983722', 'pm_score': 3, 'selected': False, 'text': '<p>I have found a nice <a href="http://www.mathworks.com/matlabcentral/fileexchange/7842-catstruct" rel="noreferrer">solution on File Exchange: catstruct</a>.</p>\n\n<p>Without testing the performance I can say that it did exactly what I wanted.\nIt can deal with duplicate fields of course.</p>\n\n<p>Here is how it works:</p>\n\n<pre><code>a.f1 = 1;\na.f2 = 2;\nb.f2 = 3;\nb.f4 = 4;\n\ns = catstruct(a,b)\n</code></pre>\n\n<p>Will give</p>\n\n<pre><code>s = \n\n    f1: 1\n    f2: 3\n    f3: 4\n</code></pre>\n'}, {'answer_id': 23767610, 'author': 'chappjc', 'author_id': 2778484, 'author_profile': 'https://Stackoverflow.com/users/2778484', 'pm_score': 3, 'selected': False, 'text': '<p>Short answer: <strong><code>setstructfields</code></strong> (if you have the Signal Processing Toolbox).</p>\n\n<hr>\n\n<p>The official solution is posted by Loren Shure on <a href="http://blogs.mathworks.com/loren/2009/10/15/concatenating-structs/" rel="nofollow noreferrer">her MathWorks blog</a>, and demonstrated by <a href="https://stackoverflow.com/a/39678/2778484">SCFrench here</a> and in <a href="https://stackoverflow.com/a/15257983/2778484">Eitan T\'s answer to a different question</a>.  However, if you have the Signal Processing Toolbox, a simple undocumented function does this already - <code>setstructfields</code>.</p>\n\n<p><strong><code>help setstructfields</code></strong></p>\n\n<pre class="lang-none prettyprint-override"><code> setstructfields Set fields of a structure using another structure\n    setstructfields(STRUCTIN, NEWFIELDS) Set fields of STRUCTIN using\n    another structure NEWFIELDS fields.  If fields exist in STRUCTIN\n    but not in NEWFIELDS, they will not be changed.\n</code></pre>\n\n<p>Internally it uses <code>fieldnames</code> and a <code>for</code> loop, so it is a convenience function with error checking and recursion for fields that are themselves structs.</p>\n\n<p><strong>Example</strong></p>\n\n<p>The "original" struct:</p>\n\n<pre><code>% struct with fields \'color\' and \'count\'\ns = struct(\'color\',\'orange\',\'count\',2)\n\ns = \n    color: \'orange\'\n    count: 2\n</code></pre>\n\n<p>A second struct containing a new value for <code>\'count\'</code>, and a new field, <code>\'shape\'</code>:</p>\n\n<pre><code>% struct with fields \'count\' and \'shape\'\ns2 = struct(\'count\',4,\'shape\',\'round\')\n\ns2 = \n    count: 4\n    shape: \'round\'\n</code></pre>\n\n<p>Calling <code>setstructfields</code>:</p>\n\n<pre><code>&gt;&gt; s = setstructfields(s,s2)\ns = \n    color: \'orange\'\n    count: 4\n    shape: \'round\'\n</code></pre>\n\n<p>The field <code>\'count\'</code> is <em>updated</em>.  The field <code>\'shape\'</code> is <em>added</em>. The field <code>\'color\'</code> <em>remains unchanged</em>.</p>\n\n<p><strong>NOTE</strong>: Since the function is undocumented, it may change or be removed at any time.</p>\n'}]
['https://Stackoverflow.com/questions/38645', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4135/']