Select columns from a table and add them into another table
57 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
fred ssemwogerere
on 8 Feb 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 Comments
fred ssemwogerere
on 8 Feb 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
More Answers (0)
See Also
Categories
Find more on Logical 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!