MATLAB Answers

0

Finding files WITHOUT a specfic prefix (or suffix, extension, etc.) using dir

Asked by D. Plotnick on 3 Jul 2018
Latest activity Answered by Guillaume
on 3 Jul 2018
Hello all, I have a question partly inspired by the question here, which asks how to find a file with a known prefix. Now, what if I want to suppress a specific prefix, but not use more complicated strfind (although I could use startsWith) operation using the 'dir' command. The obvious example is avoiding all files starting with a period: '.example.txt'.
Is there a simple way to suppress certain prefixes using 'dir'? How about suppressing suffixes or extensions (rather than exhaustively looking for the extensions you do want)?

  0 Comments

Sign in to comment.

1 Answer

Answer by Guillaume
on 3 Jul 2018
 Accepted Answer

No, the filtering capabilities offered by dir are fairly limited. If you want to exclude some pattern, you'll have to get the whole list then use string search functions such as startsWith you've mentioned or regexp. It's only one more line of code so shouldn't be any issue.
e.g. to remove all files starting with .
filelist = dir;
filelist = filelist(~startsWith({filelist.name}, '.''));

  0 Comments

Sign in to comment.