how to delete certain rows that contain string
26 ビュー (過去 30 日間)
古いコメントを表示
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'
0 件のコメント
採用された回答
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)
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 Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!