Matching the two columns of two different tables and to combine the common values in second table.

8 views (last 30 days)
I want to match two columns of different tables. One table has one column with a "station code" and other bale has 5 columns in which the first column has "station code". I want to print the final result with 5 column table with single and commmon station code.
Thank you.

Accepted Answer

Kevin Holly
Kevin Holly on 18 Aug 2022
Edited: Kevin Holly on 18 Aug 2022
You can use innerjoin or join. See Join Tables.
  3 Comments
Kevin Holly
Kevin Holly on 18 Aug 2022
t=table;
t.Column1 = rand(10,1);
t.station = {'Code1';'Code2';'Code3';'Code4';'Code5';'Code6';'Code7';'Code8';'Code9';'Code10'}
t = 10×2 table
Column1 station ________ __________ 0.5654 {'Code1' } 0.38679 {'Code2' } 0.48385 {'Code3' } 0.19474 {'Code4' } 0.11794 {'Code5' } 0.93804 {'Code6' } 0.12624 {'Code7' } 0.72142 {'Code8' } 0.81498 {'Code9' } 0.071101 {'Code10'}
t2=table;
t2.station = {'Code1';'Code3';'Code4';'Code6';'Code7'};
t2.Column2 = rand(5,1);
t2.Column3 = rand(5,1);
t2.Column4 = rand(5,1);
t2.Column5 = rand(5,1)
t2 = 5×5 table
station Column2 Column3 Column4 Column5 _________ _______ _______ ________ ________ {'Code1'} 0.36827 0.48714 0.83709 0.44785 {'Code3'} 0.4232 0.35059 0.85168 0.44361 {'Code4'} 0.28578 0.5513 0.095781 0.49893 {'Code6'} 0.44994 0.5736 0.25773 0.91444 {'Code7'} 0.2837 0.28939 0.48307 0.010173
t4 = innerjoin(t,t2)
t4 = 5×6 table
Column1 station Column2 Column3 Column4 Column5 _______ _________ _______ _______ ________ ________ 0.5654 {'Code1'} 0.36827 0.48714 0.83709 0.44785 0.48385 {'Code3'} 0.4232 0.35059 0.85168 0.44361 0.19474 {'Code4'} 0.28578 0.5513 0.095781 0.49893 0.93804 {'Code6'} 0.44994 0.5736 0.25773 0.91444 0.12624 {'Code7'} 0.2837 0.28939 0.48307 0.010173
Ali
Ali on 19 Aug 2022
Thi is compeletely correct but as I have the station codes in one table and the other has also the station codes with station name, latitute, longitude and elevation.
I want to match the station codes and those which are common to print with their respective station name, latitude, longitude and elevation.
Thank you

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!