Merge tables with duplicates

7 vues (au cours des 30 derniers jours)
Jonas Schmeiss
Jonas Schmeiss le 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 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 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 le 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 =/

Connectez-vous pour commenter.

Réponses (1)

Mischa Kim
Mischa Kim le 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 commentaires
Mischa Kim
Mischa Kim le 2 Sep 2016
Modifié(e) : Mischa Kim le 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 le 2 Sep 2016
Modifié(e) : Jonas Schmeiss le 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.

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by