Info

This question is closed. Reopen it to edit or answer.

Joining 2 tables with outer join both having extra elements

1 view (last 30 days)
I have 2 tables having names and scores of individuals. I want to find the total marks scored by people in the tests. If they took one test, it is that, else it is sum of two (or more) tests. Eg: Data is like:
A.Name = {'S';'P';'R';'M';'K'};
A.Score = [80;82;85;95;55];
>> A = struct2table(A);
B.Name = {'Mi';'J';'K'};
B.Score = [80;82;25];
B = struct2table(B);
I am trying to create a table by joining:
X = join(A, B, 'MergeKeys', true, 'Keys','Name');
X.Score_A(isNan(X.Score_A)) = 0;
X.Score_B(isNan(X.Score_B)) = 0;
X.Score = X.Score_A+X.Score_B;
Is there a more efficient way to do this ?
Sorry, I didn't mention one more step. I had to first do a union to create A, B with all elements. Else, just join fails.
  1 Comment
Ruchir Kemnaik
Ruchir Kemnaik on 6 Jul 2016
Hi Mehul,
Did the above code work for you? It will give an error since "MergeKeys" is not a valid parameter for "join" function. I used "outerjoin" function and it gave the expected result. The code I used is as follows:
X = outerjoin(A,B, 'MergeKeys', true, 'Keys','Name');
X.Score_A(isnan(X.Score_A)) = 0;
X.Score_B(isnan(X.Score_B)) = 0;
X.Score = X.Score_A+X.Score_B;

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!