how to delete certain rows that contain string

26 ビュー (過去 30 日間)
andrew
andrew 2013 年 7 月 17 日
I have the following strings within matrix m:
'batch_167_indication_A12-1_replicate_1' 'batch_167_indication_A12-1_replicate_2' 'batch_167_indication_ABC_replicate_3' 'batch_167_indication_ABC_replicate_1' 'batch_167_indication_DEF_replicate_1' 'batch_167_indication_DEF-1_replicate_1'
I only want the output to print:
'batch_167_indication_ABC_replicate_3' 'batch_167_indication_ABC_replicate_1' 'batch_167_indication_DEF_replicate_1' 'batch_167_indication_DEF-1_replicate_1'

採用された回答

Evan
Evan 2013 年 7 月 17 日
編集済み: Evan 2013 年 7 月 17 日
So you want to delete any string that contains the string 'A12-1'? Or is it more general than that? Assuming it's just that one string, try this:
C = {'batch_167_indication_A12-1_replicate_1' 'batch_167_indication_A12-1_replicate_2' 'batch_167_indication_ABC_replicate_3' 'batch_167_indication_ABC_replicate_1' 'batch_167_indication_DEF_replicate_1' 'batch_167_indication_DEF-1_replicate_1'}; % cell array of strings
Cnew = C(cellfun(@(s)isempty(regexp(s,'A12-1')),C));
If C isn't a cell array:
C = char('batch_167_indication_A12-1_replicate_1','batch_167_indication_A12-1_replicate_2','batch_167_indication_ABC_replicate_3','batch_167_indication_ABC_replicate_1','batch_167_indication_DEF_replicate_1','batch_167_indication_DEF-1_replicate_1') %character array
Cnew = C(arrayfun(@(i)isempty(regexp(C(i,:),'A12-1')),1:size(C,1)),:);
  1 件のコメント
Jos (10584)
Jos (10584) 2013 年 7 月 18 日
編集済み: Jos (10584) 2013 年 7 月 18 日
No need for regexp ...
IDX = strfind(C,'A12-1')
TF = cellfun('isempty', IDX)
Cnew = C(TF);
which can, of course, be combined into a less readable one-liner ...
Cnew = C(cellfun('isempty', strfind(C,'A12-1'))) ;

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by