how to delete certain rows that contain string

78 views (last 30 days)
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'

Accepted Answer

Evan on 17 Jul 2013
Edited: Evan on 17 Jul 2013
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 Comment
Jos (10584)
Jos (10584) on 18 Jul 2013
Edited: Jos (10584) on 18 Jul 2013
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'))) ;

Sign in to comment.

More Answers (0)


Find more on Graphics Object Properties in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!