How to vertically join differently sized tables?

5 vues (au cours des 30 derniers jours)
HWIK
HWIK le 5 Août 2021
Commenté : HWIK le 5 Août 2021
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!

Réponse acceptée

Peter Perkins
Peter Perkins le 5 Août 2021
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 commentaire
HWIK
HWIK le 5 Août 2021
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!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by