How do i combine many tables by respective column content?
    조회 수: 6 (최근 30일)
  
       이전 댓글 표시
    
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.
댓글 수: 0
채택된 답변
  Andrei Bobrov
      
      
 2017년 4월 3일
        
      편집: Andrei Bobrov
      
      
 2017년 4월 3일
  
      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
추가 답변 (1개)
  Guillaume
      
      
 2017년 4월 3일
        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
참고 항목
카테고리
				Help Center 및 File Exchange에서 Configure Simulation Conditions에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


