How to split the name in to two cells

2 ビュー (過去 30 日間)
Kanakaiah Jakkula
Kanakaiah Jakkula 2017 年 9 月 28 日
編集済み: Cedric 2017 年 9 月 28 日
Hi,
I have a cell matrix:
'56' 'mat find false' '89 mm' 'mat 96 kl'
I want to split:
  1. 'mat find false' --> 'mat' 'find false' (one cell to two cells)
  2. 'mat 96 kl' -->'mat' '96 kl'
desired Output:
'56' 'mat' 'find false' '89 mm' 'mat' '96 kl'
Many thanks in advance,

採用された回答

Cedric
Cedric 2017 年 9 月 28 日
編集済み: Cedric 2017 年 9 月 28 日
Or regexp-based: if
C = {'56', 'mat find false', '89 mm', 'mat 96 kl'} ;
then
result = regexp(C, '(mat)?\s?(.*)', 'tokens', 'once') ;
result = [result{:}] ;
result(cellfun(@isempty, result)) = [] ;
outputs:
result =
1×6 cell array
'56' 'mat' 'find false' '89 mm' 'mat' '96 kl'
  2 件のコメント
Jan
Jan 2017 年 9 月 28 日
As usual I mention that cellfun('isempty') is faster than cellfun(@isempty), because it does not call a Matlab function in a loop, but checks the emptiness inside the Mex function.
Cedric
Cedric 2017 年 9 月 28 日
編集済み: Cedric 2017 年 9 月 28 日
Thanks Jan!
I'll have to frame the following on my wall:

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

その他の回答 (1 件)

Rik
Rik 2017 年 9 月 28 日
編集済み: Rik 2017 年 9 月 28 日
Easy to solve with looping through the list from the end:
for n=length(c):-1:1
idx=strfind(c{n},' ');
if ~isempty(idx)
c((n+1):(end+1))=c(n:end);
c{n}(idx(1):end)=[];
c{n+1}(1:idx(1))=[];
end
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by