-1

I have to read out numbers and possibly some letters from large set of file names in a directory. The file names have a format as "aXXXX_bXX_XX_S.ext" where 'X' could be any number and 's' could be any letter or a string. How do I extract those numbers and string as separate cell array?

Thanks!

NeuronGeek
  • 47
  • 5
  • Welcome to StackOverflow! You should show us what you already tried. For now, take a look at [`strsplit`](http://www.mathworks.com/help/matlab/ref/strsplit.html) or [`strtok`](http://www.mathworks.com/help/matlab/ref/strtok.html). Good luck! – Dev-iL Apr 26 '16 at 21:28
  • You want a [regular expression](http://www.mathworks.com/help/matlab/ref/regexp.html?refresh=true). I suggest checking out the documentation and playing around on [regex101](https://regex101.com/) to figure out the correct pattern. – sco1 Apr 26 '16 at 22:23

1 Answers1

0

First you can read all the files inside your directory. Assuming the location of your folder is stored in the string path, use:

 a=dir(mypath);

Now you have a structure a. File names are stored in a.name. Now you can work with it. Here is a very rough code. You loop over all the files, check if the first letter is a (there could be some hidden files, you don't need them). Then you extract the data you need from the eligible files.

 n=0;
 for i=1:numel(a)
     if a(i).name(1)=='a'
         n=n+1; 
         numbers{n}=strcat(a(i).name(2:5),a(i).name(8:9),a(i).name(11:12));
         letters{n}=a(i).name(13:find(a(i).name=='.')-1);

     end
 end
Mikhail Genkin
  • 3,247
  • 4
  • 27
  • 47