Effacer les filtres
Effacer les filtres

How do i combine many tables by respective column content?

1 vue (au cours des 30 derniers jours)
ahmed obaid
ahmed obaid le 3 Avr 2017
Dear Experiences ...
i have three tables ( table A, table B and table C)
table A look like following where first columns contain unique names, and second column contain public names (may contain repeated names)..
name sun_name
____________
Amy 'tomy'
Bob 'tomy'
Hol 'salmon'
Har 'cookies'
Sal 'pizza'
second column B look like following
sun_name var1(B).....varn(B)
____________ _______________
'tomy' 0.0 .... 4.5
'tomy' 4.5 .... 5.2
'salmon' etc..
'cookies'
'pizza'
table C look like following
sun_name var1(c).....varm(c)
____________ _______________
'tomy' 0.0 .... 4.5
'tomy' 4.5 .... 5.2
'salmon' etc..
'cookies'
'pizza'
result table (out) perhaps include all columns .. as follow: out
name sun_name var1(B)...varn(B) var1(C)...varm(C)
the problem here content of name field in table A must be repeated based on other tables ? how to do that please.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 3 Avr 2017
Modifié(e) : Andrei Bobrov le 3 Avr 2017
Maybe so?
Table_out = [A,B(:,2:end),C(:,2:end)];
Added:
As said by Guillaume about innerjoin:
A = readtable('1.xls');
B = readtable('2.xls');
C = readtable('3.xls');
Tout = sortrows(innerjoin(A,innerjoin(B,C)),1);
  2 commentaires
Image Analyst
Image Analyst le 3 Avr 2017
Try various join methods, like join and outerjoin.
ahmed obaid
ahmed obaid le 3 Avr 2017
ok, i have treated join tables and reach to my goal .. thanks a lot

Connectez-vous pour commenter.

Plus de réponses (1)

Guillaume
Guillaume le 3 Avr 2017
I don't see how your excel tables relate to your original question. There's no repeated key in any of them.
Going back to your original question, it looks like you want innerjoin. I'm not too clear on the relationship between B and C. Are you supposed to have 4 'tomy' after joining these two? Or do B and C have the same number of row and must simply be concatenated:
  • option 1: innerjoin
D = innerjoin(B, C);
  • option 2: plain concatenation (probably?)
D = [B, C(:, 2:end)];
The join with A is an innerjoin
A = table({'Amy'; 'Bob'; 'Hol'; 'Har'; 'Sal'}, {'tomy'; 'tomy'; 'salmon'; 'cookies'; 'pizza'}, 'VariableNames', {'name', 'sun_name'})
B = table({'tomy'; 'tomy'; 'salmon'; 'cookies'; 'pizza'}, [0; 4.5; 2; 3; 4], 'VariableNames', {'sun_name', 'var1b'})
C = table({'tomy', 'tomy', 'salmon', 'cookies', 'pizza'}', (10:10:50)', 'VariableNames', {'sun_name', 'var1c'})
D = innerjoin(A, [B, C(:, 2:end)])
results in:
7×4 table
name sun_name var1b var1c
_____ _________ _____ _____
'Har' 'cookies' 3 40
'Sal' 'pizza' 4 50
'Hol' 'salmon' 2 30
'Amy' 'tomy' 0 10
'Amy' 'tomy' 4.5 20
'Bob' 'tomy' 0 10
'Bob' 'tomy' 4.5 20

Catégories

En savoir plus sur Language Fundamentals dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by