# Matlab: join two cell arrays or structures

How is it possible in Matlab to join two cell arrays or structures?

I've a first cell array (or structure):

Name A1 A1 B1 C2 C2

a second cell array (or structure):

Name Value Type A1 1 a B1 56 b C1 12 c C2 58 c C3 45 c C4 15 c

I need to get this result:

Name Value Type A1 1 a A1 1 a B1 56 b C2 58 c C2 58 c

Thanks

## Answers

If the data are cell arrays: use the second output of ismember:

cell1 = {'A1';'A1';'B1';'C2';'C2'}; % example data cell2 = {'A1',1;'B1',56;'C1',12;'C2',58;'C3',45;'C4',15}; % example data [ii jj] = ismember(cell1,cell2(:,1)); result = cell2(jj(ii),:)

If the data are structure matrices: you only need small modifications to the code above:

mat1.Name = ['A1';'A1';'B1';'C2';'C2']; % example data mat2.Name = ['A1';'B1';'C1';'C2';'C3';'C4']; % example data mat2.Value = [1; 56; 12; 58; 45; 15]; % example data mat2.Type = ['a';'b';'c';'c';'c';'c']; % example data [ii jj] = ismember(mat1.Name,mat2.Name,'rows'); result.Name = deal(mat2.Name(jj(ii),:)); result.Value = deal(mat2.Value(jj(ii),:)); result.Type = deal(mat2.Type(jj(ii),:));