Splitting Cell Arrays by Delimiter

조회 수: 16 (최근 30일)
Lydia Wang
Lydia Wang 2019년 3월 14일
편집: Stephen23 2019년 3월 14일
So I have a large data set stored in a 4699x1 cell. Each row has a different number of string items separate by commas. Is there any way to count how many items there are in each row? For example, with these rows, I would like a 4699x1 cell that is 3,4,1,1,2,2,2,1,1,1,4,etc...Screen Shot 2019-03-14 at 5.37.53 PM.png

채택된 답변

Star Strider
Star Strider 2019년 3월 14일
Try this:
s = {'qwerty,uiop'; 'asdf,ghjkl,zxcvb'};
for k1 = 1:size(s,1)
r(k1) = numel(strsplit(s{k1}, ','));
end
Out = r % View Results (Delete)
producing:
Out =
2 3
Experiment to get the result you want.

추가 답변 (1개)

Stephen23
Stephen23 2019년 3월 14일
편집: Stephen23 2019년 3월 14일
There is no need to split, just count the commas, e.g.:
>> s = {'AAAA,BBBB'; 'CCCC,DDDD,EEEE'; 'FFFF'};
>> n = 1+cellfun(@numel,strfind(s,','))
n =
2
3
1
or
>> n = 1+cellfun(@(v)nnz(v==','),s)
n =
2
3
1

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by