How do you vertically concatenate two tables with different variables of different data types

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

"...but the ones that do match need to line up in the same columns". In your example, which table columns/variables are supposed to match? Given your output (merged) table, I don't see the logic behind your desired match.

Sign in to comment.

Answers (1)

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)
T = 5×5 table
string1 double1 double2 string2 double3 _______ _______ _________ _________ _________ "Tesla" 10 0.45316 "Super" 0.22514 "ford" -10 0.67852 "none" 0.0028847 "ford" 50 0.0024394 <missing> NaN "mazda" -30 0.90388 "Sub" 0.29036 "mazda" 30 0.2278 "Sub" 0.21008

3 Comments

Bora is correct that outerjoin can be used to do this specific example. However, if the two input tables "overlap", i.e. the same combination of "key" values appears in a row in both tables, things can start getting funny, depending on what you want to happen. If you know that's not the case, you are OK.
Thanks for the recommendation. The outerjoin works. I should have recalled that option.
Peter is correct. The results with the real tables which are much larger yeild different results depending on the order. But understanding of that is available in the documentation and trial and error.

Sign in to comment.

Categories

Products

Release

R2021b

Tags

Asked:

on 12 Dec 2022

Commented:

on 13 Dec 2022

Community Treasure Hunt

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

Start Hunting!