How to vertically join differently sized tables?

5 ビュー (過去 30 日間)
HWIK
HWIK 2021 年 8 月 5 日
コメント済み: HWIK 2021 年 8 月 5 日
Hi,
Lets say I have two tables:
table1=table(1,70,175,29,datetime(1992,7,1),"M",'VariableNames',{'Num','Weight','Height','Age','Birth','Gender'})
table2=table(2,175,29,'VariableNames',{'Num','Height','Age'})
Where table1 has 6 variables and table2 has 3, but all 3 of those appear in table1. Would it be possible to vertically concatenate these two and fill the missing variables in table2 with NaN or NaT for doubles and datetimes?
Any help is appreciated,
Thanks!

採用された回答

Peter Perkins
Peter Perkins 2021 年 8 月 5 日
One way is to "assign off the end":
>> table1 = table(1,70,175,29,datetime(1992,7,1),"M",'VariableNames',{'Num','Weight','Height','Age','Birth','Gender'})
table2 = table(2,175,29,'VariableNames',{'Num','Height','Age'})
table1 =
1×6 table
Num Weight Height Age Birth Gender
___ ______ ______ ___ ___________ ______
1 70 175 29 01-Jul-1992 "M"
table2 =
1×3 table
Num Height Age
___ ______ ___
2 175 29
>> table1((end+1):(end+height(table2)),table2.Properties.VariableNames) = table2
Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables are extended with rows containing
default values.
> In tabular/subsasgnParens (line 482)
In tabular/subsasgn (line 64)
table1 =
2×6 table
Num Weight Height Age Birth Gender
___ ______ ______ ___ ___________ _________
1 70 175 29 01-Jul-1992 "M"
2 0 175 29 NaT <missing>
But that is perhaps not the most controlled way to do it. You might be better off just adding the missing variables to the second table.
  1 件のコメント
HWIK
HWIK 2021 年 8 月 5 日
Yes, adding it manually might be best after all, if not the missing values are assigned some default values, not necessarily the ones I'd be looking for. But thanks for the help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by