Splitting an array into variable lengths depending on it's content

조회 수: 5 (최근 30일)
Matt Darby
Matt Darby 2014년 3월 12일
답변: Vishal Rane 2014년 3월 12일
I have an array containing S sections of data that I need to extract and create S new arrays each containing one of those sections. The start of each section has the string 'E0=' and is followed by the data I need (though this amount varies between section). i.e. if there are N elements, strf{2} is a cell
strf{2} = [E0= 5 614 82 97 E0= 91 44 7 E0= 54 774 624 6339 4 1...]
So far I have found each element of the array where 'E0=' is using this code:
dEpres = strcmp(strf{2}, 'E0=');
S = sum(dEpres);
dEelems = find(dEpres, S);
Such that dEelems = [1 6 10 ...].
What I want to do now is split this array into n arrays at the elements where 'E=0' is so I have:
array1 = [E0= 5 614 82 97] array2 = [E0= 91 44] array3 = [E0= 54 774 624 6339 4 1]
I have tried to use some sort of for loop to generate array(i) where i=1:S but haven't been successful.
Any help would be really appreciated!
Thanks, Matt

답변 (1개)

Vishal Rane
Vishal Rane 2014년 3월 12일
If str = 'E0= 5 614 82 97 E0= 91 44 7 E0=54 774 624 6339 4 1...'
[matchstart,~,~,~,tokenstring,~,~] = regexp( str, 'E0=\s|E0=', 'split')
matchstart =
'' '5 614 82 97 ' '91 44 7 ' '54 774 624 6339 4 1...'
tokenstring =
'E0= ' 'E0= ' 'E0='
matchstart(1) = []
cellfun(@horzcat, tokenstring, matchstart, 'un', 0)
ans =
'E0= 5 614 82 97 ' 'E0= 91 44 7 ' 'E0=54 774 624 6339 4 1...'

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by