How to divide a string into substrings
2 ビュー (過去 30 日間)
古いコメントを表示
Hi
How can I fragment a string into substring? For example
Str = 'ABCDEF'
How can i fragment the above string in into following :
'A', 'BCDEF'
'AB' , 'CDEF'
'ABC' , 'DEF'
'ABCD' , 'EF'
'ABCDE' , 'F'
and strore substrings into a struct?
0 件のコメント
採用された回答
madhan ravi
2019 年 4 月 27 日
編集済み: madhan ravi
2019 年 4 月 27 日
Assuming your using version higher than 2016b of MATLAB:
Str='A':'F';
Z=repmat(Str,strlength(Str)-1,1);
f = @(x)regexp(""+x,'\w*','match');
ZZ=[f(tril(Z)),f(triu(Z,1))];
S=cell2struct(ZZ,{'First_Part','Second_Part'},2)
For older versions:
Str='ABCDEF';
Z=repmat(Str,strlength(Str)-1,1);
f = @(x)regexp(cellstr(x),'\w*','match');
ZZ=[f(tril(Z)),f(triu(Z,1))];
S=cell2struct(reshape(ZZ,[],2),{'First_Part','Second_Part'},2)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!