I'd like to merge two different tables on matlab, how do I do that?

16 次查看(过去 30 天)
Hello to anyone reading this,
I have two excel tables which I'd like to merge into one big table / array. Basically, they all have different columns, and different number of rows and lines. The only similar thing is the first column, which is time (in seconds). Some values are similar in both tables, while others are different.
If possible, I'd like to merge the two tables into one big table without losing any data. Example of what I have / want:
Have:
Time1 = ["t1";"t2";"t3"];
value1 = [374; 164; 476];
value2 = [2455;5478;2354];
value3 = [53782;35683;24682];
table1 = table(Time1, value1, value2, value3)
table1 = 3×4 table
Time1 value1 value2 value3 _____ ______ ______ ______ "t1" 374 2455 53782 "t2" 164 5478 35683 "t3" 476 2354 24682
Time2 = ["t1";"t4"];
value4 = [257;31572];
value5 = [247;3578];
table2 = table(Time2, value4, value5)
table2 = 2×3 table
Time2 value4 value5 _____ ______ ______ "t1" 257 247 "t4" 31572 3578
I'd like to get:
Time3 = ["t1";'t2';'t3';'t4'];
value1 = [374; 164; 476;""];
value2 = [2455;5478;2354;""];
value3 = [53782;35683;24682;""];
value4 = [257;"";"";31572];
value5 = [247;"";"";3578];
table3 = table(Time3, value1, value2, value3, value4, value5)
table3 = 4×6 table
Time3 value1 value2 value3 value4 value5 _____ ______ ______ _______ _______ ______ "t1" "374" "2455" "53782" "257" "247" "t2" "164" "5478" "35683" "" "" "t3" "476" "2354" "24682" "" "" "t4" "" "" "" "31572" "3578"
I'd appreciate any help/advice with that, thanks in advance!
Important: my table contains both numbers and words.
(Small disclaimer: i'm fairly new to matlab so don't know much about it yet)
  2 个评论
Anat Lazikin
Anat Lazikin 2023-3-27
any type of join gives me this error:
"Unable to concatenate the table variables 'Seconds_s_' and 'Marker', because their types are double and cell."
my table1 has both numbers and words, I'm guessing this is the issue but i must have them both

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2023-3-27
Time1 = ["t1";"t2";"t3"];
value1 = [374; 164; 476];
value2 = [2455;5478;2354];
value3 = [53782;35683;24682];
table1 = table(Time1, value1, value2, value3,'Var',{'Time','V1','V2','V3'})
table1 = 3×4 table
Time V1 V2 V3 ____ ___ ____ _____ "t1" 374 2455 53782 "t2" 164 5478 35683 "t3" 476 2354 24682
Time2 = ["t1";"t4"];
value4 = [257;31572];
value5 = [247;3578];
table2 = table(Time2, value4, value5,'Var',{'Time','V4','V5'})
table2 = 2×3 table
Time V4 V5 ____ _____ ____ "t1" 257 247 "t4" 31572 3578
table3 = outerjoin(table1,table2,'MergeKeys',1)
table3 = 4×6 table
Time V1 V2 V3 V4 V5 ____ ___ ____ _____ _____ ____ "t1" 374 2455 53782 257 247 "t2" 164 5478 35683 NaN NaN "t3" 476 2354 24682 NaN NaN "t4" NaN NaN NaN 31572 3578
  2 个评论
Anat Lazikin
Anat Lazikin 2023-3-27
I tried but it gave me the error "Unable to concatenate the table variables 'Seconds_s_' and 'Marker', because their types are double and cell."
Only reason I can imagine is that "marker" contains words, while "seconds" has numbers?
Walter Roberson
Walter Roberson 2023-3-27
The implication of the error message is that Seconds_s_ and Marker each occur in both tables, but in one of the tables they are double and in the other table they are cell .
When you have the same variable in both tables, then in order to join the tables, the variable must be compatible types in the two tables.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by