How can I concatenating 2 table

2 views (last 30 days)
JAEMIN LEE
JAEMIN LEE on 24 Apr 2018
Edited: Sterling Baird on 5 Sep 2020
left = table(['A0';'A1';'A2'],['B0';'B1';'B2'],... 'VariableNames',{'A' 'B'},... 'RowNames',{'K0' 'K1' 'K2'}); right = table(['C0';'C2';'C3'],['D0';'D2';'D3'],... 'VariableNames',{'C' 'D'},... 'RowNames',{'K0' 'K2' 'K3'});
Allname = union(left.Properties.RowNames, right.Properties.RowNames); result = zeros(length(Allname),size(left,2)+size(right,2)) * NaN; table_result= array2table(result); table_result.Properties.VariableNames = [left.Properties.VariableNames right.Properties.VariableNames]; table_result.Properties.RowNames=Allname; index_A =ismember(Allname, left.Properties.RowNames); index_B =ismember(Allname, right.Properties.RowNames); table_result(index_A,left.Properties.VariableNames) = A <- ERROR
I want to solve the problem. Give them a hand.

Answers (3)

Ameer Hamza
Ameer Hamza on 24 Apr 2018
The column entries of the table must belong to the same class, therefore you can't use nan since it is double, whereas other elements are char arrays. In the following code, I have replaced it with char array 'nan'. Other than that, the following script will work as required.
left = table(['A0';'A1';'A2'],['B0';'B1';'B2'],...
'VariableNames',{'A' 'B'},...
'RowNames',{'K0' 'K1' 'K2'});
right = table(['C0';'C2';'C3'],['D0';'D2';'D3'],...
'VariableNames',{'C' 'D'},...
'RowNames',{'K0' 'K2' 'K3'});
Allname = union(left.Properties.RowNames, right.Properties.RowNames);
result = repmat({'nan'}, length(Allname), size(left,2)+size(right,2)); %zeros(length(Allname),size(left,2)+size(right,2)) * NaN;
table_result= array2table(result);
table_result.Properties.VariableNames = [left.Properties.VariableNames right.Properties.VariableNames];
table_result.Properties.RowNames=Allname;
table_result(left.Properties.RowNames,left.Properties.VariableNames) = table2cell(left);
table_result(right.Properties.RowNames,right.Properties.VariableNames) = table2cell(right);


Sterling Baird
Sterling Baird on 5 Sep 2020
Edited: Sterling Baird on 5 Sep 2020
outerjoin() works in the case you described (no common variable names between the tables), but when that's not the case, it will produce different behavior than what you seem to be going for. For example, with these two tables as inputs:
nrows = 3;
%define table columns
doubles = rand(nrows,1);
chars = repelem('a',nrows,1);
cells = repelem({rand(10)},nrows,1);
%make two tables
tbl1 = table(doubles,chars)
tbl2 = table(chars,cells)
>>
tbl1 =
3×2 table
doubles chars
________ _____
0.75906 a
0.017267 a
0.49258 a
tbl2 =
3×2 table
chars cells
_____ ______________
a {10×10 double}
a {10×10 double}
a {10×10 double}
outerjoin(tbl1,tbl2) will produce the following:
9×4 table
doubles chars_tbl1 chars_tbl2 cells
________ __________ __________ ______________
0.75906 a a {10×10 double}
0.75906 a a {10×10 double}
0.75906 a a {10×10 double}
0.017267 a a {10×10 double}
0.017267 a a {10×10 double}
0.017267 a a {10×10 double}
0.49258 a a {10×10 double}
0.49258 a a {10×10 double}
0.49258 a a {10×10 double}
As a simple modification to outerjoin, you can assign a list of temporary IDs that you remove at the end. You just need to makes sure that 'ids' (or whatever you choose) doesn't overlap with a variable in one of the tables.
t1names = tbl1.Properties.VariableNames;
t2names = tbl2.Properties.VariableNames;
sharednames = intersect(t1names,t2names);
tbl1.ids = (1:3).';
tbl2.ids = (4:6).';
tbloutjoin = outerjoin(tbl1,tbl2,'Key',['ids',sharednames],'MergeKeys',true);
removevars(tbloutjoin,'ids')
>>
doubles chars cells
_______ _____ ______________
0.24666 a { 0×0 double}
0.08822 a { 0×0 double}
0.47188 a { 0×0 double}
NaN a {10×10 double}
NaN a {10×10 double}
NaN a {10×10 double}
You may also consider using FEX: tblvertcat, which builds on outerjoin() following a similiar method as shown and supports 2+ tables: tblvertcat(tbl1,tbl2)
doubles chars cells
________ _____ ______________
0.75906 a { 0×0 double}
0.017267 a { 0×0 double}
0.49258 a { 0×0 double}
NaN a {10×10 double}
NaN a {10×10 double}
NaN a {10×10 double}

Community Treasure Hunt

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

Start Hunting!