Filter löschen
Filter löschen

Join 3 tables based on 2 key fields, trouble using outerjoin()

1 Ansicht (letzte 30 Tage)
newbie9
newbie9 am 6 Apr. 2020
Kommentiert: newbie9 am 6 Apr. 2020
How can I combine 3 tables into 1 table, using two "key" fields? I have tried various combinations of outerjoin() without any luck. Thanks in advance for any help, I'm sure I'm missing something obvious. Using Matlab R2018a, no fancy add-ons.
Code
%%% set up dummy data tables
Key1 = [1 1 1 2 2 3 3 3 3 3];
Key2 = [1 2 3 1 2 1 2 3 4 5];
Val1 = [0 NaN NaN 0 NaN 0.09 NaN NaN NaN NaN];
Val2 = [NaN 0.55 0.55 0.04 0.04 0.58 0.634 0.668 0.6950 0.7560];
mytable = array2table([Key1', Key2', Val1', Val2']);
mytable.Properties.VariableNames = {'Key1', 'Key2', 'Val1', 'Val2'};
temp1 = array2table([1 4 0; 2 3 0; 3 6 0.09]);
temp1.Properties.VariableNames = {'Key1', 'Key2', 'Val1'};
temp2 = array2table([1 4 0.55; 2 3 0.04; 3 6 0.07560]);
temp2.Properties.VariableNames = {'Key1', 'Key2', 'Val2'};
%%% try to join mytable, temp1, and temp2 together
Tout = outerjoin(mytable, temp1, 'MergeKeys', true);
Tout = outerjoin(Tout, temp2, 'MergeKeys', true);
Current Result
I want the highlighted rows to be combined, such that the Key1-Key2 pair is not duplicated in the output table.
Example Desired Result
  1 Kommentar
newbie9
newbie9 am 6 Apr. 2020
Got it-- I just needed to switch the order:
Tout = outerjoin(temp1, temp2, 'MergeKeys',true);
Tout = outerjoin(mytable, Tout, 'MergeKeys',true);

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by