2016-12-20 14 views
1

Pen Driveの文字列と空のセル[]を含むセル(mx1)を削除します。例えばセル配列からいくつかの行を削除し、新しいセル配列を作成します。

:私はセル配列がある場合:

S_Block = {   [] 
         [] 
      'D\My Folder\Amazing\Pen Drive' 
      'C\Go To\Where\Home' 
      'H\I am\No Where\Hostel' 
      'F\Somewhere\Appartment\Pen Drive' 
      'Ram\North\Sky\Pen Drive' 
      'L\South\Pole\Ice' 
         [] 
      'Go\East\Japan\Pen Drive'} 

を次に新しいセルアレイが含まれている必要があります

Snew_Block = { 'C\Go To\Where\Home' 
       'H\I am\No Where\Hostel' 
       'L\South\Pole\Ice' } 

答えて

3
Snew_Block = S_Block;  
% Removing empty contents 
Snew_Block(cellfun(@isempty,Snew_Block)) = []; 
% Removing the rows having the specified string 
Snew_Block(~cellfun(@isempty,strfind(Snew_Block,'Pen Drive'))) = []; 

あなたはR2016bを持っている場合は、新しい機能containsがあり、これがための論理値を返します。そう、これは何かが空でないことを試験する二重否定するよりも簡単で読みやすくするために

% Removing the rows having the specified string 
Snew_Block(contains(Snew_Block,'Pen Drive')) = [] 

のように記述することができる指定された文字列を含む行を削除する文字列の一致があります。

+0

ありがとう –

0

は、ここでは、多くの可能な解決策のものを見つけます。それが空の場合にも、検索トークンが内部にある場合、私はすべての文字列をチェックするために、ループを使用している:

toDelete = []; 
for i=1:length(S_Block) 
    % check for the string if it's empty 
    if isempty(S_Block{i}) 
     % store the index of the string 
     toDelete = [toDelete i]; 
     continue; 
    end; 
    % search for the token 'Pen Drive' 
    if ~isempty(strfind(S_Block{i},'Pen Drive')) 
     % store the index of the string 
     toDelete = [toDelete i]; 
    end; 
end; 

% delete all found strings from the cell-array 
S_Block(toDelete) = []; 
+0

あなたのコードでは、変数 'toDelete'はループ内のサイズを変更し続けます。 [ここをクリック](https://www.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html)なぜそれが良いとは思われないのです –

関連する問題