How to vertically concatenate two tables with different sizes

155 ビュー (過去 30 日間)
Mars
Mars 2017 年 2 月 3 日
編集済み: Sterling Baird 2020 年 9 月 5 日
For example:
>> A = table(['A1';'A1';'A1'],[1;1;1],[1;1;1],...
'VariableNames',{'Var1' 'Var2' 'Var3'})
A =
Var1 Var2 Var3
____ ____ ____
A1 1 1
A1 1 1
A1 1 1
>> B = table(['B1';'B1'],[2;2],[2;2],...
'VariableNames',{'Var1' 'Var2' 'Var4'})
B =
Var1 Var2 Var4
____ ____ ____
B1 2 2
B1 2 2
How can I vertically concatenate A and B like the following? Thanks!
Var1 Var2 Var3 Var4
____ ____ ____ ____
A1 1 1 NAN
A1 1 1 NAN
A1 1 1 NAN
B1 2 NAN 2
B1 2 NAN 2
  1 件のコメント
Mars
Mars 2017 年 2 月 3 日
Thank you, Walter! It works!

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2017 年 2 月 3 日
outerjoin(A,B,'MergeKeys', true)
It took me a few minutes to find the right command.

その他の回答 (1 件)

Sterling Baird
Sterling Baird 2020 年 9 月 5 日
編集済み: Sterling Baird 2020 年 9 月 5 日
Also consider looking at FEX: tblvertcat which builds on outerjoin and supports 2+ tables and shared variable names
tblvertcat(A,B)
>>
Var1 Var2 Var3 Var4
____ ____ ____ ____
A1 1 1 NaN
A1 1 1 NaN
A1 1 1 NaN
B1 2 NaN 2
B1 2 NaN 2

カテゴリ

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by