How to Remove files that don't match string?

6 ビュー (過去 30 日間)
Ibro Tutic
Ibro Tutic 2017 年 5 月 10 日
コメント済み: dpb 2017 年 5 月 10 日
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.

回答 (2 件)

Guillaume
Guillaume 2017 年 5 月 10 日
The easiest, assuming R2016b or later, is to use contains:
filteredarray = yourcellarray(contains(yourcellarray, 'Z1P') & contains(yourcellarray, 'Z1P') & contains(yourcellarray, 'Z1G'))
  4 件のコメント
Ibro Tutic
Ibro Tutic 2017 年 5 月 10 日
Would it be possible to do this on a structure?
Guillaume
Guillaume 2017 年 5 月 10 日
If by structure you mean the structure returned by dir:
filteredstruct = dirstruct(~isempty(regexp({dirstruct.name}, 'Z1P|Z2P|Z1G', 'once')));

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


dpb
dpb 2017 年 5 月 10 日
編集済み: dpb 2017 年 5 月 10 日
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 件のコメント
Ibro Tutic
Ibro Tutic 2017 年 5 月 10 日
Not sure if you might have just miss-named that variable, but those are the strings that I want. i.e. if the file has Z1P, keep it or Z2P keep it, etc.
dpb
dpb 2017 年 5 月 10 日
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 ~.

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

カテゴリ

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