How to combine 2 tables with different variables into 1 table?
Show older comments
I would like to combine Table1 and Table2 into Table3 as shown below. Any advice on how it can be done?
I have used the below commands, but not successful.
Method 1
Table3 = join(Table1,Table2,'Keys','SysTime')
The above is not successful as the key variable for B must contain all values in the key variable for A.
Method 2
Table3 = [Table1 Table2]
The above is not successful due to the duplicate name 'SysTime'.

2 Comments
To anyone reading this in the future: this is very simple to do with just one command.
Doing this by converting the tables to numeric matrix, sorting, identifying the matching elements via ISMEMBER and then creating a new table... is indirect, complex, obfuscated, and inefficient. Much better is to read the MATLAB documentation:
and read about the functions listed in the section "Join and Set Operations".
Jayden Yeo
on 21 Sep 2021
Accepted Answer
More Answers (2)
The actual MATLAB solution just takes one simple line of code:
SysTime = [1;2;3;4;5];
EDF = [10;20;30;40;50];
T1 = table(SysTime,EDF)
SysTime = [1;1.5;2;2.5;5];
MDF = [2;4;5;6;7];
T2 = table(SysTime,MDF)
T3 = outerjoin(T1,T2, 'MergeKeys',true) % this is all you need.
3 Comments
Jayden Yeo
on 21 Sep 2021
Stephen23
on 21 Sep 2021
@Yeo Swee Huei: you can vote for my answer too, if it helped you.
Jayden Yeo
on 21 Sep 2021
Note that join() works very well, but you should correctly define the two table to be joined. See this example:
A = (1:5)'; B = A*10; C = A+B; D = C/2; Item = (1:5)';
ABC = [Item, A, B]; CBD = [Item, C, D];
T1 = array2table(ABC, 'VariableNames', {'Item', 'A', 'B'});
T2 = array2table(CBD, 'VariableNames', {'Item', 'C', 'D'});
TT = join(T1, T2)
1 Comment
Jayden Yeo
on 21 Sep 2021
Categories
Find more on Multirate Signal Processing 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!