MATLAB Answers

How can I concatenate or merge two structures?

1,312 views (last 30 days)
I would like to merge two structures into a new structure containing all the fields of the two original structures. How can I do this in MATLAB?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
There is no direct ability in MATLAB that can be used to concatenate structures.
The attached file mergeStructs.m shows a number of methods that can be used to merge structures in MATLAB.
There are also online submissions on the MATLAB Central User Community that you can use. One such submission is :
Note that MathWorks does not guarantee or warrant the use or content of these submissions. Any questions, issues, or complaints should be directed to the contributing author.

  1 Comment

Royi Avital
Royi Avital on 23 May 2020
Is there a way which is MATLAB Codeer friendly?

Sign in to comment.

More Answers (4)

James
James on 23 Aug 2016
You can do this manually:
f = fieldnames(structA);
for i = 1:length(f)
structB.(f{i}) = structA.(f{i})
end

  4 Comments

Show 1 older comment
Dan
Dan on 15 Jan 2019
Thanks for the function.
I found a small error. Line near the end should be:
merged_struct(size_a+j).(f{i}) = struct_b(j).(f{i});
David Walwark
David Walwark on 29 Sep 2019
I second Dan on this. Please edit the answer so other people don't waste their time.

Sign in to comment.


John Beaumont
John Beaumont on 12 Sep 2017
Convert structures to tables, then merge tables, then convert resulting table back to a structure.
% Create 1st structure
aa_s.val1 = 1;
aa_s.val2 = 2;
% Create 2nd structure
bb_s.val3 = 3;
bb_s.val4 = 4;
% Convert structures to tables
aa_t = struct2table( aa_s );
bb_t = struct2table( bb_s );
% Concatonate tables
merge_t = [ aa_t ,bb_t ];
% Convert table to structure
merge_s = table2struct( merge_t )

  0 Comments

Sign in to comment.


Ba Mo
Ba Mo on 12 Nov 2019
This works IF AND ONLY IF there are no common fields (duplicate fields) in the 2 structures.
mergestructs = @(x,y) cell2struct([struct2cell(x);struct2cell(y)],[fieldnames(x);fieldnames(y)]);
I don't see why nobody pointed this out. it's intuitive!

  1 Comment

Ganesh Mohan
Ganesh Mohan on 25 May 2020
Most underrated comment. Worked like a charm!

Sign in to comment.


Salomon Muller
Salomon Muller on 21 Feb 2018
This is not a solution. Since to merge tables the number of rows need to be the same as well.

  1 Comment

Peter Perkins
Peter Perkins on 21 Feb 2018
The original question is somewhat ill posed - it might refer to either two scalar structs or two struct arrays. Those are two different things, used in different contexts.
Salomon, you are correct that if the two structs are scalars, each field might contain vectors with different lengths (or even more general things), and converting to a table won't work.
But if the two structs are scalars whose fields all contain the same height vectors, or if the two structs are arrays, and in each element each field contains a scalar value, then struct2table/table2struct will work.
In fact, in either of those cases, it might even be better to stop half-way, and just use the table. One of the benefits of tables over scalar structs and struct arrays for tabular data is that tables allow for straight-forward subscripting in both dimensions. An Nx1 struct array with M fields supports convenient subscripting for "rows", but a scalar struct with M fields each containing an Nx1 vector doesn't make subscripting rows for more than one field easy at all. And neither support arbitrary subscripting across multiple fields.

Sign in to comment.

Products


Release

R2006b