how to delete certain rows that contain string
25 次查看(过去 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!