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

2 vues (au cours des 30 derniers jours)
newbie9
newbie9 le 6 Avr 2020
Commenté : newbie9 le 6 Avr 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 commentaire
newbie9
newbie9 le 6 Avr 2020
Got it-- I just needed to switch the order:
Tout = outerjoin(temp1, temp2, 'MergeKeys',true);
Tout = outerjoin(mytable, Tout, 'MergeKeys',true);

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by