Select columns from a table and add them into another table

조회 수: 83 (최근 30일)
BN
BN 2020년 2월 8일
댓글: BN 2020년 2월 8일
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
fred ssemwogerere 2020년 2월 8일
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
fred ssemwogerere 2020년 2월 8일
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 2020년 2월 8일
Hello, Thank you. It works like a charm

댓글을 달려면 로그인하십시오.

추가 답변 (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