How to Remove files that don't match string?
Mostrar comentarios más antiguos
I have a list of filenames, and I am trying to remove any files that don't have the string 'Z1P', 'Z2P', or 'Z1G' in them.
The file names are stored in a cell array called Files.
Thanks for any help.
Respuestas (2)
Guillaume
el 10 de Mayo de 2017
filteredarray = yourcellarray(contains(yourcellarray, 'Z1P') & contains(yourcellarray, 'Z1P') & contains(yourcellarray, 'Z1G'))
4 comentarios
Ibro Tutic
el 10 de Mayo de 2017
Guillaume
el 10 de Mayo de 2017
If it's an or, then
filteredarray = yourcellarray(contains(yourcellarray, {'Z1P', 'Z2P', 'Z1G'}));
In versions < R2016b, the simplest way may be to use regexp:
filteredarray = yourcellarray(~isempty(regexp(yourcellarray, 'Z1P|Z2P|Z1G', 'once')));
Ibro Tutic
el 10 de Mayo de 2017
Guillaume
el 10 de Mayo de 2017
If by structure you mean the structure returned by dir:
filteredstruct = dirstruct(~isempty(regexp({dirstruct.name}, 'Z1P|Z2P|Z1G', 'once')));
If anywhere in the filename, then
UnwantedStrings={'Z1P', 'Z2P', and 'Z1G'};
Files=Files(~ismember(upper(Files),'UnwantedStrings);
If it's an extension or needs must be in the filename and not extension, use fileparts first to separate pieces needed.
3 comentarios
Guillaume
el 10 de Mayo de 2017
I don't thing that this is going to work. ismember does not find strings within strings.
If it were to work, the order within ismember would need to change.
Ibro Tutic
el 10 de Mayo de 2017
dpb
el 10 de Mayo de 2017
Guillaume -- DOH! you're right. regexpi it is...
Ibro--oh, ok, it doesn't work anyway, but read it as to keep everything but. So, if it worked otherwise, lose the ~.
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!