# Advanced MATLAB: File name processing from directory listing10

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.

Video Content

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.

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

regards,
david

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

David,

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.

Doug

Doug replied on : 4 of 10

Matt,

That is an excellent suggestion. I just posted it to our internal enhancement tracking database. You can make such requests more reliably by e-mailing support@mathworks.com

Thanks,
Doug

Kaija replied on : 5 of 10

Hi

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?

/Kaija

My filename is tpjp199202.mon and my commands are:

files=dir(‘*.mon’);
name=files(1).name;
regexp(name,’tpjp(\d*).mon’,’tokens’);
ans

ans =

{1×1 cell}

ans(1)

ans =

{1×1 cell}

Doug replied on : 6 of 10

@Kaija,

Use {} not ()

Doug

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
http://sohowww.nascom.nasa.gov//data/REPROCESSING/Completed/2011/c2/20110101/
I want to list these files, just the image files.

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