File Exchange Pick of the Week

Our best user submissions

Advanced MATLAB: File name processing from directory listing 10

Posted by Doug Hull,

Very often MATLAB users want to be able to process a set of files from a directory. In this video, I show how you can look through a directory seeking files of the form “data###.dat” and then returning the “###” as a number with any leading zeros removed. This makes use of regular expressions, but does not explain their theory much at all.

This is not the first time this kind of thing has been discussed. Check out Loren’s take, or Steve’s take from their blogs.


Comments are closed.

10 CommentsOldest to Newest

david replied on : 1 of 10

nice. just a thought:
rather than trying ideas in the command window, one idea is to do this using the editor in cell mode. that way, when you’ve figured out the right syntax, you almost have a finished script. this also saves scrolling through the command history for the correct version of each command which after trial-and-error often look similar to versions that don’t quite work


matt fig replied on : 2 of 10

Off topic, but David’s reply made me think about an enhancement I would like to see. Imagine scrolling up through the command history, either through the editor or by using the up arrow. When you came across a command that caused an error, it would be in red font. Boy that would make life simpler when looking through several similar commands, some of which didn’t work.

Doug replied on : 3 of 10


Actually, I tend to do a little bit of both. It is hard to say when I work at the command line and then script and when I write a script and just keep running it as you add to it.


Kaija replied on : 5 of 10


I’m trying to get the number from my filename, and I’m doing exactly as shown in the video, but when I type ans(1) my answer is 1×1 cell.
Do you know what I’m doing wrong?


My filename is tpjp199202.mon and my commands are:


ans =

{1×1 cell}


ans =

{1×1 cell}

KASA replied on : 7 of 10

Hello guys
Do you have any idea of how can I list files from the web site?
for example, I have many images that have this form 20110101_0000_c2_512.jpg to be processed the only thing changes is 20110101_THIS_c2_512.jpg and it has to be 4 digits start from 0000 to 2354
this is the website
I want to list these files, just the image files.
your help is appreciated in advance

Yuri replied on : 8 of 10

Doug, a small correction. ‘*’ in regular expressions means 0 or more. It is ‘+’ that mean 1 or more. So if we have a file ‘data.dat’, it will also match your regexp returning empty string as a token.

Yuri replied on : 9 of 10

By the way, if someone need just a list of filenames, it can be done in several ways without for-loop.

namelist = ls('*.dat'); % as a char array
namelist = cellstr(ls('*.dat')); % as a cell array of strings
namelist = {files(:).name}'; % from dir output
Deepti replied on : 10 of 10

How can i store data from txt files onto a string in matlab? I have a bunch of dat files that serve as a database,and they have to be compared to an input file(text file) .
This is the code snippet i’ve managed to come up with but the importdata line shows an error sayin fullfile cannot be used for input of type struct. a holds a 5*1 structure

f=[‘e:’ filesep ‘dfiles’];
for i=1:5