View License

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

» Watch video

Highlights from

4.7 | 7 ratings Rate this file 6 Downloads (last 30 days) File Size: 1.82 KB File ID: #2118 Version: 1.4




14 Aug 2002 (Updated )

Do a recursive search for files.

| Watch this File

File Information

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".

MATLAB release MATLAB 6.1 (R12.1)
Other requirements Tested only on Win 2000
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
21 Dec 2010 Sami Kanderian

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

Comment only
08 Sep 2010 K E

K E (view profile)

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

Comment only
06 Jul 2010 K E

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)

10 Jun 2010 Nitin

Nitin (view profile)

Works great... fast as well

25 Jun 2009 Marco

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.

Comment only
29 May 2009 Steen Johansen

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

16 Apr 2009 Shi vera

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

28 Aug 2007 Max Brugel

Best thing since sliced bread.

22 Jul 2005 Jerry Wagner

Very good. Saved me a lot of time.

15 Aug 2002 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!

07 Apr 2009 1.1

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.

16 Apr 2009 1.2

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

28 May 2009 1.3

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.

28 May 2009 1.4

Bug Fix

Contact us