How to Remove files that don't match string?
    조회 수: 9 (최근 30일)
  
       이전 댓글 표시
    
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.
댓글 수: 0
답변 (2개)
  Guillaume
      
      
 2017년 5월 10일
        filteredarray = yourcellarray(contains(yourcellarray, 'Z1P') & contains(yourcellarray, 'Z1P') & contains(yourcellarray, 'Z1G'))
댓글 수: 4
  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
      
      
 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
  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 Exchange에서 Characters and Strings에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

