How to vertically join differently sized tables?
조회 수: 5 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!