How do you vertically concatenate two tables with different variables of different data types
23 views (last 30 days)
Show older comments
I have two tables with datetime, double, and string variables. I would like to vertically concatenate them.
The tables do not have the same variables, but the ones that do match need to line up in the same columns or it is hard to compare the value in table1 with the value in table2.
I found this thread, but it creates doubles for all of the new variables, which throws an errror with the strings and datetimes.
I tried creating a new table with the same number of rows as table1, and same variables missing from table2. But this requires typing the variables into my script
adder_table = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);
but I don't know how to get the VariableTypes of the difference between the two tables.
I would prefer to not type the names of the variables into my script, as my data sets might change as the initiative adds or drops variables.
Table1
string1 double1 double2
ford 50 0.002439379
Table2
string1 string2 double1 double2 double3
mazda Sub 30 0.227799138 0.210079407
Tesla Super 10 0.453158897 0.225137845
ford none -10 0.678518656 0.002884727
mazda Sub -30 0.903878415 0.290356264
Table_want
string1 string2 double1 double2 double3
ford 50 0.002439379
mazda Sub 30 0.227799138 0.210079407
Tesla Super 10 0.453158897 0.225137845
ford none -10 0.678518656 0.002884727
mazda Sub -30 0.903878415 0.290356264
4 Comments
Bora Eryilmaz
on 12 Dec 2022
Yup. That is what's called an outer table join: https://www.mathworks.com/help/matlab/ref/table.outerjoin.html.
Answers (1)
Bora Eryilmaz
on 12 Dec 2022
Edited: Bora Eryilmaz
on 12 Dec 2022
C1 = {"ford" 50 0.002439379};
T1 = cell2table(C1, "VariableNames", ["string1" "double1" "double2"]);
C2 = {...
"mazda" "Sub" 30 0.227799138 0.210079407; ...
"Tesla" "Super" 10 0.453158897 0.225137845; ...
"ford" "none" -10 0.678518656 0.002884727; ...
"mazda" "Sub" -30 0.903878415 0.290356264};
T2 = cell2table(C2, "VariableNames", ["string1" "string2" "double1" "double2" "double3"]);
T = outerjoin(T1, T2, 'MergeKeys',true)
3 Comments
See Also
Categories
Find more on Logical 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!