File Exchange Pick of the Week

February 26th, 2008

MATLAB Basics: Getting a directory listing

In the past we covered how to use UIGETFILE as an alternative to hardcoding the name of a file in your program. [click here] In this video, we will show how MATLAB can get a directory listing and use it to batch process the files in a directory.

Video Content

iconFiles.jpgiconPod.jpglazy.jpg

15 Responses to “MATLAB Basics: Getting a directory listing”

  1. mans replied on :

    Hello,
    I know that it is off topic but I hope I can an answer. Which software do you use to create these videos? They are very well created and they are very informative.

    Best regards
    Mans

  2. Doug replied on :

    Mans,

    I use Camtasia.

    http://www.techsmith.com/camtasia.asp

    Doug

  3. Russell W replied on :

    Wow! I work in computational psychology and really prefer to keep my subject’s data in individual .mat files with their subject numbers. Until now, when I aggregated the data I would physically move copies of the .mat files into new directories. Now I can keep them all in one and select them in a loop based on certain criteria. Awesome! You just saved me weeks of work!

  4. Doug replied on :

    Russell,

    Thanks for letting me know. We like to hear that these blogs are helpful to people. Be sure to subscribe to this, and the other MathWorks blogs for more great ideas!

    -Doug

  5. Ben replied on :

    Hey,

    Thank you for the tutorial but I must ask, once you have the files required in the array, how do you parse (read) them? You seemed to have a generic function to do it bit I am unsure how to extract the name from the array to read the data in.

    Thanks

  6. Doug replied on :

    Ben,

    The name is in the name field as you saw in the video:

    files(1).name

    will give you just the name of the first file.

    Depending on the files you are reading, you would use different functions, like CSVREAD, XLSREAD, IMREAD

    What kind of files are you trying to read?

    Doug

  7. sundar naganathan replied on :

    Hi
    I have a list of folders and inside each folder i have a list of tif files which I have to process. I am now able to read all the tif files using a combination of ‘dir’ command and ‘imread’ command in a m-file.
    My intention is to go into first folder, read all the tif files, process them, then come out. Then I will have to read the second folder, read all the tif files and process them and so on. Can you tell me how to open a folder and come out of a folder ? Basically I need an alternative for cd command to be used in a m-file.

    Thanks
    sundar

  8. ShaneAhmed replied on :

    The command dir is useful for the current directory.
    for example, If I have to open a file which is not in the current directory but located at some path ‘D:\abc\’, ‘D;\abcd’ and of type .tif
    now using the code
    a = dir(‘D:\abc\*.tif’) or a = dir(‘D:\abcd\*.tif’)
    a(1).name only returns the name of the file and not the path
    I will have to append the path with the name using strcat to open it.
    I think fileattrib will be more useful here
    the following code works for the above problem by sundar

    [b, a] = fileattrib('D:\abc\*.tif');
    if(b)
    S = size(a)
    for i = 1:S(1,2)
    imread(a(i).Name);
    end
    end
    [b, a] = fileattrib('D:\abcd\*.tif');
    if(b)
    S = size(a)
    for i = 1:S(1,2)
    imread(a(i).Name);
    end
    end

    sundar you can go with the above code

  9. stephane hordoir replied on :

    Thanks a lot Doug !
    Your example also works with directories !

  10. D replied on :

    The naming system of this function is an epic fail.

    For example it takes as 2190 is smaller than 920. Because it is starting with 2!

  11. Anne Rionta replied on :

    I’ve been trying to list only the folders in the path, that is to say, I want to exclude files and just list the folders.

    I have been able to do this using the unix command line from matlab using:

    [Status,Folders]=unix(‘ls -d */| grep -v private’);

    However, I would like my scripts to be portable to windows systems so I would like to find a native solution in matlab.

    Any ideas?

    Thanks in advance,

  12. Mike Katz replied on :

    @Anne,

    You can take advantage of the DIR function which lists all files/folders in the directory and eliminate the non directories:

    x = dir;
    justdirs = x([x.isdir])
    dirnames = {justdirs.name}
    
  13. madhusudan replied on :

    my mmreader is not working also the problem is aviread is giving warnings and not playing larger average quality video

  14. jc replied on :

    I would like to find tiff images within several folders. Once located, I want to copy them in a new directory. Is there a short way to do this?

  15. sunny replied on :

    Hi,

    I have a simple question, I would like to extract the directory name from the file path given.
    For eg: getDir(‘../testDir/test.dat’);

    i want to get back only the dir name in this case ‘../testDir/’.
    Is there a way to do it in matlab.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


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 The MathWorks.