How to split a cell array by finding certain cells

4 ビュー (過去 30 日間)
Stephen Liederbach
Stephen Liederbach 2015 年 8 月 3 日
Say I have a cell that is something like
testCell = {'start', 1, 2, 3, 'start', 4, 5, 'start', 6, 7, 8, 9}
How can I split it into several separate cells so that I get something like
test1 = {'start', 1, 2, 3}
test2 = {'start', 4, 5}
test3 = {'start', 6, 7, 8, 9}
In the final program the length of the original cell, as well as the number of times 'start' appears isn't constant, and the data in between is sometimes numbers, sometimes strings, and occasionally has some empty cells (which can't be deleted)
Any help would be appreciated

採用された回答

Matt J
Matt J 2015 年 8 月 3 日
編集済み: Matt J 2015 年 8 月 3 日
This is perhaps the closest thing to your request without incurring the problems discussed in this thread. In other words, instead of separate variables test1, test2, test3,... I've given you test{1}, test{2}, test{3}, ...
a=strcmp(testCell,'start');
z=cumsum(a);
N=sum(a);
test=cell(1,N);
for i=1:N, test{i}=testCell(z==i); end
  1 件のコメント
Stephen Liederbach
Stephen Liederbach 2015 年 8 月 4 日
Thanks, that should work

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2015 年 8 月 3 日
編集済み: Andrei Bobrov 2015 年 8 月 3 日
accumarray(cumsum(strcmp(testCell,'start'))',...
(1:numel(testCell))',[],@(x){testCell(x)})

カテゴリ

Help Center および File ExchangeTesting Frameworks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by