I would like to create a for loop which can merge variables dynamically by first letter, excluding the number standing behind the letter.
from this:
to this:
Thanks!

 채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 13일

1 개 추천

names = YourTable.Properties.VariableNames;
initials = extractBefore(names, 2);
[G, id] = findgroups(initials);
newTable = table();
for K = 1 : length(id)
newTable.(id{K}) = [YourTable{:,G==K}];
end

댓글 수: 2

Alexandra Kopaleyshvili
Alexandra Kopaleyshvili 2021년 6월 13일
Hey Walter, thank you very much!
Is it possible to combine those multicolumn variables (like B or L) into single-column-variables (as shown in the second picture)?
Alexandra Kopaleyshvili
Alexandra Kopaleyshvili 2021년 6월 13일
편집: Alexandra Kopaleyshvili 2021년 6월 13일
Ok i got it! I just needed to add this to your code:
for jj=1:width(newTable)
size(newTable.(jj))
if ans(1,2)> 1
alinear = {newTable.(jj){:}}'
{cat(1, alinear{:})}
newTable.(jj)=ans
end
end

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by