File Exchange

image thumbnail


version 1.4 (1.82 KB) by

Do a recursive search for files.

7 Ratings



View License

Returns a cell array of all files matching the wildcard expression "refiles" beginning in the root folder "root". Includes subdirectories.

mfiles = getfilenames('c:\','*.m') will return all .m files on the c drive to the cell array "mfiles".

Comments and Ratings (10)

To make this code work for Matlab 2010b as well as other versions for both PC and Mac replace the lines 30 to 34:

if ispc
elseif ismac



Now it will work with version 2010b


K E (view profile)

No longer works recursively on R2010b, even thought it does on R2010a. I am not sure why.


K E (view profile)

Useful. Wish Matlab had more builtins for file searching and management, since I write mfiles that have to be used by Windows users (no perl)


Nitin (view profile)

Works great... fast as well


Marco (view profile)

Does not work for linux, because it uses the function "ismac" instead of isunix.
Additionally, I do not see why the wildcard should be set to an empty string on a mac, a '*' would make more sense, right?

Why not use the Matlab-original function 'what'? At least for Matlab-related files, it gives you exactly what you want to do with getfilenames.

Very nice. It is surprisingly fast. This will save me a lot of time.

Shi vera

it works well in Matlab2008a(7.6).Thanks for sharing!

Max Brugel

Best thing since sliced bread.

Jerry Wagner

Very good. Saved me a lot of time.

anonymous anonymous

Nice file, but one request - Can you rewrite this so that instead of CD-ing between directories, your recursive calls to DIR include the right path information? This way if the program errors or is halted (say, with cntrl-c), you don't end up in some strange directory. This would also make the program more robust to potential network problems, too (eg, such as getting "abandoned" in a stale filesystem if a network drive is suddenly disconnnected after you've CD-ed into it).

If you can do that, I'd give this file a 4 or 5!



Bug Fix


Thanks to Kenn Sebesta. His Comments:

Hi, I fixed getfilenames.m to work with Mac. I'm betting it works with
Linux now, too.

The secret ingredient was `fullfile`, which lets Matlab decide for
itself how to format dir calls.


Bug fix on line 36. Changed 'cd' to 'root'. Thanks Elena.


Removed the cd to other folders per suggestion. Now, it stays in the folder where it was called. Also added ability to simply return all subfolder names only if a single root folder argument is passed.

MATLAB Release
MATLAB 6.1 (R12.1)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video