Select columns from a table and add them into another table

68 vues (au cours des 30 derniers jours)
BN
BN le 8 Fév 2020
Commenté : BN le 8 Fév 2020
Hey all,
I have C1 and C2. Each one is (1 x 2) means that containing 2 tables inside. I want to copy column numbers 5, 6, and 8 from all tables in C1 to the exact table in C2 (after the first column of C2 and before other columns).
I think I should use vertcat and then use groupsummary but unfortunately don't know how to do that.
As my C1 and C2 have a very large size I just cut part of them and attached them, original C1 and C2 are 1 x 85.
% This is not a code but I want something like this
%copy C1{1, 1}.column5,column6,column8
%paste to C2{1, 1}>>> in front of first existing column
%copy C1{1, 2}.column5,column6,column8
%paste to C2{1, 2}>>> in front of first existing column
Any advice is truly helpful
Thank you so much
Best regards

Réponse acceptée

fred  ssemwogerere
fred ssemwogerere le 8 Fév 2020
Hello, you don't need to use "groupsummary" or "vertcat" for this. Instead use; "addvars". I think this should do nicely:
% Taking "C2" to be the cell with tables you are copying into, using data from tables in cell "C1"
for i=1:size(C2,1)
for j=1:size(C2,2)
% Replace "Var5", "Var6", and "Var8" with names of your table column variables in cell "C1" at positions: 5,6,and 8 as you mentioned
C2{i,j}=addvars(C2{i,j},C1{i,j}.Var5,C1{i,j}.Var6,C1{i,j}.Var8,'Before',2);
% "2" specifies to insert the data after the first column
end
end
  3 commentaires
fred  ssemwogerere
fred ssemwogerere le 8 Fév 2020
Hello, all that is not required, just make this small edit to the code above on line 3 (I have added a field: "NewVariableNames"). Change the input arguments; "Var5","Var6", "Var8", according to the names you want to use
% Taking "C2" to be the cell with tables you are copying into, using data from tables in cell "C1"
for i=1:size(C2,1)
for j=1:size(C2,2)
% Replace "Var5", "Var6", and "Var8" with names of your table column variables in cell "C1" at positions: 5,6,and 8 as you mentioned
C2{i,j}=addvars(C2{i,j},C1{i,j}.Var5,C1{i,j}.Var6,C1{i,j}.Var8,'Before',2,'NewVariableNames',{'Var5','Var6','Var8'});
% "2" specifies to insert the data after the first column
end
end
BN
BN le 8 Fév 2020
Hello, Thank you. It works like a charm

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by