How do i combine many tables by respective column content?

1 visualización (últimos 30 días)
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.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 3 de Abr. de 2017
Editada: Andrei Bobrov el 3 de Abr. de 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 comentarios
Image Analyst
Image Analyst el 3 de Abr. de 2017
Try various join methods, like join and outerjoin.
ahmed obaid
ahmed obaid el 3 de Abr. de 2017
ok, i have treated join tables and reach to my goal .. thanks a lot

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 3 de Abr. de 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

Categorías

Más información sobre Language Fundamentals en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by