Copy and re-arrange data from one column into another

조회 수: 7 (최근 30일)
DavidL88
DavidL88 2021년 1월 28일
댓글: J. Alex Lee 2021년 2월 1일
Hi
I have a table with a column of data all like this - Extract clusters: FCL (2 files) | CG_MLD_11
I'd like to copy a part of this data into another column but in this structure - CG_MLD_11_FCL
Also in another table I have data in a column like this - Perm t-test equal [100ms,250ms 100, 101]: Avg: 11_right | bl | _HLG_MLD (20) vs. Avg: 11_right | bl | _CG (15) | FCL
I similarly would like to copy some of this data into another column in the same format as above - CG_MLD_11_FCL
The content varies but the structure is the same e.g. CG might be MOD, or FCL might be CPL, or 11 might be 21, etc. but all in the same format as above.
Can anyone advise how I do this?
  댓글 수: 11
DavidL88
DavidL88 2021년 1월 31일
I worked it out using a combination of insert (/) and split functions. Not the most elegant solution but appropriate for a convoluted mess :)
J. Alex Lee
J. Alex Lee 2021년 2월 1일
dpb, I don't know if this is what you had in mind but:
TSplit = array2table(strtrim(split(Tsample.Var1,'|')))
parts = cellfun(@(s)split(s,"_"),TSplit.Var2,'UniformOutput',false)
gives the broken up pieces in a cell array of cell arrays.
I would think for purposes of this problem that's still not helpful because not only is the demarcator symbol "_", but also some of the experiment identifiers contain the same character within them, e.g., CH_MLD. I guess if it is important to actually split the string into the parts, you'd have to use a matching strategy as you identified, and I guess regexp could work although as you mentioned it would be pretty challenging to do it in one go.

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

채택된 답변

J. Alex Lee
J. Alex Lee 2021년 2월 1일
Is this what you are looking for?
load("~/Downloads/Sample_table_MatLab.mat")
a = ["CG", "CG_LOW", "CG_HGH", "MLD", "MLD_LOW", "MLD_HGH", "MOD", "MOD_LOW", "MOD_HGH"];
b = ["11", "12", "21", "22"];
c = ["FCL", "FCR", "CPL", "CPR", "POL", "POR"];
TSplit = array2table(strtrim(split(Tsample.Var1,'|')))
for i = 1:height(TSplit)
matches = regexp(TSplit.Var1{i},cellstr(c),"match");
TSplit.Var3(i) = matches{~cellfun('isempty',matches)};
end
TSplit.Var4 = string(TSplit.Var2)+"_"+string(TSplit.Var3)
  댓글 수: 2
DavidL88
DavidL88 2021년 2월 1일
Yes that was it! Thank you.
J. Alex Lee
J. Alex Lee 2021년 2월 1일
Ok, glad that was it. But the more general problem if things to the right of "|" had not already been in order is circumvented here, which only extracts one of the substrings specified in "c" from the left side of "|" and joins it to the right side with "_".

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scripts에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by