How do you vertically concatenate two tables with different variables of different data types
조회 수: 23 (최근 30일)
이전 댓글 표시
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
Bora Eryilmaz
2022년 12월 12일
Yup. That is what's called an outer table join: https://www.mathworks.com/help/matlab/ref/table.outerjoin.html.
답변 (1개)
Bora Eryilmaz
2022년 12월 12일
편집: Bora Eryilmaz
2022년 12월 12일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!