File Exchange

image thumbnail

folderFiles.m

version 1.3 (1.89 KB) by

Reads filename contents of a folder.

4 Downloads

Updated

View License

[outFiles]=folderFiles(inFolder,fileNames)
Reads a group of (fileNames) from a folder specified by (inFolder), and generates the output in the form of a char array.
fileNames is the criteria that specifies the files, e.g., *.jpg. If fileNames is left as a blank (default) this function reads all contents, but this is not preferred since some folders containing images will also have system files such as (Thumbs.db) or (folder.ini) beside subfolders.

Comments and Ratings (3)

Belal

Belal (view profile)

thanks

Thanks a lot Jan, these are useful comments. However, there is a line of code at the very end:

rmpath(inFolder);

which removes the newly added directory from the path, just in case it confuses the original (user pre-set) Matlab path.

Once again, this simple function reads the (contents of a folder)-if not specified, these contents could be files and sub-folders.

My regards.

Jan Simon

Jan Simon (view profile)

Adding a folder temporarily to the Matlab PATH can have dangerous side-effects. As far as I can see, this is not needed at all, because you use the absolute path name already: "fileNames = fullfile(inFolder)". Here "fullfile" is actually useless, such that I'm confused.
Your code does not distinguish folders and files, but the name of the function and the output implies, that only files are returned.

CHAR matrices are not very useful in Matlab, such that I think this would be better:
function out = folderFiles(varargin)
d = dir(fullfile(vargargin{:}));
out = {d([d.isdir]==0).name};

I like the documentation of this function: (almost) a H1-Line (delete the leading empty comment line), description of inputs and outputs, examples, date and author, "See also" line. This is very good!

Updates

1.3

1)Unnecessary bits of code are removed.
2)Two new error-check lines are added to detect input arguments' type.
3)File description is slightly modified.

MATLAB Release
MATLAB 7.8 (R2009a)

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

» Watch video

Win prizes and improve your MATLAB skills

Play today