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

1 回表示 (過去 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 Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by