File Exchange Pick of the Week

September 9th, 2008

Advanced MATLAB: File name processing from directory listing

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

iconFiles.jpg

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.

10 Responses to “Advanced MATLAB: File name processing from directory listing”

  1. david replied on :

    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

  2. matt fig replied on :

    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.

  3. Doug replied on :

    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

  4. Doug replied on :

    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

  5. Kaija replied on :

    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}

  6. Doug replied on :

    @Kaija,

    Use {} not ()

    Doug

  7. KASA replied on :

    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.
    your help is appreciated in advance

  8. Yuri replied on :

    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.

  9. Yuri replied on :

    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
    
  10. Deepti replied on :

    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

    g=fileread(‘e:\…’);
    f=['e:' filesep 'dfiles'];
    a=dir(f);
    data=zeros(size(g),15);
    for i=1:5
    data(i)=importdata(fullfile(f,a(i)));

    end


MathWorks

Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

These postings are the author's and don't necessarily represent the opinions of MathWorks.