tags:

views:

85

answers:

4

I want to read files from a directory in matlab. Then I need to check whether it is file or directory and then do processing. Can someone provide me a code snippet for that? or please suggest a link to do that?

+1  A: 

Maybe take a look at the MathWorks site, they always have some nice examples and helpful remarks. Eg:

FileOperations

mropa
+3  A: 

The function DIR returns an array of structures, one for each element of the directory. One of the structure members is a flag called isdir.

mydir = 'c:\test';
allentries = dir(mydir); % array of all files and dirs within target
diridxs = [allentries.isdir];
alldirs = allentries(diridxs); % array of all the dirs
allfiles = allentries(~diridxs); % array of all the files
for ctr = 1:length(allfiles)
    disp(allfiles(i).name)

Note that the directory entries include . and .. which can be confusing when you're trying to recursively parse a directory tree...

mtrw
+4  A: 

I wrote a blog that addresses at least part of your problem: http://blogs.mathworks.com/loren/2006/08/02/processing-a-set-of-files/

--Loren

Loren
+1  A: 

I did a video on something that might apply:

http://blogs.mathworks.com/videos/2008/09/09/advanced-matlab-file-name-processing-from-directory-listing/

MatlabDoug

related questions