Select columns from a table and add them into another table

89 次查看(过去 30 天)
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

采纳的回答

fred  ssemwogerere
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 个评论
fred  ssemwogerere
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by