Merge tables with duplicates

7 views (last 30 days)
Jonas Schmeiss
Jonas Schmeiss on 2 Sep 2016
Edited: Jonas Schmeiss on 2 Sep 2016
Hi, I couldn't find an answer to my problem yet so here we go.
There are 2 big tables(1800x505 and a 1800x309 table), lets call them table1 and table2. I want to merge them to 1 big table.
The problem is that in those tables there are Columns with the same name for example "CO2". in the first table with data like {5;2;2;4} and in the second with {4;3;9;1} If I want to plot "CO2" later over a time{1;2;3;4} I want to see both graphs.
so how do I merge those tables to fill "CO2" with botch sets of data? Is it possible?
for testing here is the small sample code:
table1 = {5;2;2;4};
table2 = {4;3;9;1};
table1=table(table1,'VariableNames',{'CO2'});
table2=table(table2,'VariableNames',{'CO2'});
now I want to merge those but not sure how
table=[table1,table2]; or table= join(table1,table2); neither works or gives me the result I want
  2 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 2 Sep 2016
This not clear. To illustrate, post two tables,for example: 8x7 and 6x4, then post the expected result
Jonas Schmeiss
Jonas Schmeiss on 2 Sep 2016
tables must have the same height otherwise I cant plot them.
I just want to use the same function like I have in cells
z{1}=[5;2;2;4];
z{2}=[4;3;9;1];
time=[1;2;3;4];
plot(time,cell2mat(z))
maybe im bad at explaning =/

Sign in to comment.

Answers (1)

Mischa Kim
Mischa Kim on 2 Sep 2016
Hi Jonas, why not rename the columns of one of the tables, or even both? E.g. CO21 and CO22. See this answer.
  3 Comments
Mischa Kim
Mischa Kim on 2 Sep 2016
Edited: Mischa Kim on 2 Sep 2016
Understood. Again, you can merge the tables and then create the plot with only one command. Does this satisfy your needs?
CO21 = [5;2;2;4];
CO22 = [4;3;9;1];
time = [1;2;3;4];
table1 = table(CO21,'VariableNames',{'CO21'});
table2 = table(CO22,'VariableNames',{'CO22'});,
table3 = table(time,'VariableNames',{'Time'});
table = [table1 table3 table2];
plot(time,table2array(table(:,strmatch('CO2',table.Properties.VariableNames))));
Jonas Schmeiss
Jonas Schmeiss on 2 Sep 2016
Edited: Jonas Schmeiss on 2 Sep 2016
yes probably will do, just need to find a loop now that renames those variable names while i try to put the tables together and a duplicate appears.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!