필터 지우기
필터 지우기

Find cells that start with text and combine it with next cell

조회 수: 1 (최근 30일)
HT
HT 2019년 9월 13일
댓글: Stephen23 2019년 9월 13일
I have a cell array like:
{'Test1'}
{'10v'}
{'20va'}
{'30v'}
{'50v'}
{'65v'}
{'Test2'}
{'80v}
{100v}
I want to find cells that start with text and combine that with next cell and store in same variable. Output should be something like:
{'Test1 10v'}
{'20va'}
{'30v'}
{'50v'}
{'65v'}
{'Test2 80v'}
{100v}

채택된 답변

Stephen23
Stephen23 2019년 9월 13일
편집: Stephen23 2019년 9월 13일
>> C = {'Test1';'10v';'20va';'30v';'50v';'65v';'Test2';'80v';'100v'}
C =
'Test1'
'10v'
'20va'
'30v'
'50v'
'65v'
'Test2'
'80v'
'100v'
>> X = cellfun('isempty',regexp(C(:),'^\D','once'));
>> Y = cumsum([1;X(1:end-1)]);
>> F = @(x){sprintf(' %s',C{x})};
>> Z = strtrim(accumarray(Y,(1:numel(C)).',[],F))
Z =
'Test1 10v'
'20va'
'30v'
'50v'
'65v'
'Test2 80v'
'100v'
  댓글 수: 2
Stephen23
Stephen23 2019년 9월 13일
@David Hill: thank you! It can be simplified with strjoin:
>> Z = accumarray(Y,(1:numel(C)).',[],@(x){strjoin(C(x))})
Z =
'Test1 10v'
'20va'
'30v'
'50v'
'65v'
'Test2 80v'
'100v'

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

추가 답변 (1개)

David Hill
David Hill 2019년 9월 13일
Cell array (C), combined cell array (c).
i=1;
j=1;
while i<=size(C,2)
a=cell2mat(C{i})-57;
if a(1)>0
c{j}=[cell2mat(C{i}),' ',cell2mat(C{i+1})];
i=i+2;
else
c{j}=C{i};
i=i+1;
end
j=j+1;
end

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by