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

17 Ansichten (letzte 30 Tage)
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 Kommentare

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Bora Eryilmaz
Bora Eryilmaz am 12 Dez. 2022
Bearbeitet: Bora Eryilmaz am 12 Dez. 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)
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 Kommentare
Ted HARDWICKE
Ted HARDWICKE am 13 Dez. 2022
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by