## concatStruct

version 1.10.0.0 (3.43 KB) by
Concatenate two structures as vectors or arrays.

Updated 15 Jul 2010

% Concatenates two structures into a new structure. They do not have to have the same fields.

This works for vector structures or structure arrays. Also works for nested structures.

Currently can only concatenate along 1 dimension across all fields if concatenating a vector structure.

% Example1 - vectors:
%
% Structure S1 has three fields, a, b, c. Structure S2 has 2 %fields, a and c. a and b are vectors, c is a cell array.
%
% S1.a = 1:10;
% S1.b = 2;
% S1.c = {'a', 'b', 'c'};
% S2.a = 11:20;
% S2.c = {'d', 'e', 'f'};
%
% S3 = concatStruct(S1,S2,'vec',2);
% S3 =
%
% a: [2x10 double]
% b: 2
% c: {2x3 cell}
%
%
% Example2 - structure arrays:
%
% S1(1).a = 1:10;
% S1(1).b = 1:10;
% S1(2).a = 2;
% S1(3).b = {'a', 'b', 'c'};
% S2(1).a = 11:20;
% S2(2).a = {'d', 'e', 'f'};
%
% S3 = concatStruct(S1,S2,'array',2);
% S3 =
%
% 1x5 struct array with fields:
% a
% b
%
%
% Example3 - structure arrays with mismatched dimensions:
%
% S1(1).a = 1:10;
% S1(1).b = 1:10;
% S1(2).a = 2;
% S1(3).b = {'a', 'b', 'c'};
%
% S1 =
%
% 1x3 struct array with fields:
% a
% b
%
% S2(1).a = 11:20;
% S2(2).a = {'d', 'e', 'f'};
% S2(2,1).a = 3;
%
% S2 =
%
% 2x2 struct array with fields:
% a
%
% S3 = concatStruct(S1,S2,'array',1);
% S3 =
%
% 3x3 struct array with fields:
% a
% b
%
% S4 = concat(S1,S2,'array',2);
% S4 =
%
% 2x5 struct array with fields:
% a
% b

### Cite As

Gennady Erlikhman (2022). concatStruct (https://www.mathworks.com/matlabcentral/fileexchange/28193-concatstruct), MATLAB Central File Exchange. Retrieved .

##### MATLAB Release Compatibility
Created with R2008b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux
##### Acknowledgements

Inspired by: unique_no_sort

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!