Permalink
Browse files

updated list_files_in_directory to have an optional extensions parameter

  • Loading branch information...
1 parent da2939d commit d154479c0497f135514ad5f5efcf3f69ef2780be @cvondrick cvondrick committed Jan 27, 2012
Showing with 24 additions and 12 deletions.
  1. +24 −12 util/list_files_in_directory.m
View
36 util/list_files_in_directory.m
@@ -1,24 +1,36 @@
-function files = list_files_in_directory(dirpath)
-% Given a directory, list all files inside the directory and create
-% a cell array of strings, where each string is the file location
-% NOTE: this can be used with convert_to_I, as follows
-% Examples:
-% >> files = list_files_in_directory('~/myimages/');
-% >> imagesc(convert_to_I(files{10}))
+function files = list_files_in_directory(dirpath, extensions)
+% Given a directory, list all files inside the directory and create a cell
+% array of strings, where each string is the file location
%
-% In the case where there are non-image files in the directory
-% >> files = list_files_in_directory('~/myimages/*jpg');
-% >> imagesc(convert_to_I(files{10}))
+% extensions is optional and a cell array of extensions that you want this
+% function to return. If given, any file with an extension not listed in this
+% cell array will be not be returned. If extensions is true, then only images
+% will be returned.
+%
+% NOTE: this can be used with convert_to_I, as follows >> files =
+% list_files_in_directory('~/myimages/'); >> imagesc(convert_to_I(files{10}))
if ~isstr(dirpath)
fprintf(1,'list_files_in_directory: Warning, not a directory\n');
files = {};
return;
end
-[basedir, other, other2] = fileparts(dirpath);
files = dir(dirpath);
isdirs = arrayfun(@(x)x.isdir,files);
files = files(~isdirs);
files = {files.name};
-files = cellfun2(@(x)[basedir '/' x],files);
+files = cellfun2(@(x)[dirpath '/' x],files);
+
+if exist('extensions', 'var'),
+ if ~iscell(extensions),
+ extensions = {'.jpg', '.png', '.gif', '.ppm', '.tif', '.jpeg'};
+ end
+ ext = cellfun2(@(x)get_extension(x), files);
+ ext = cellfun2(@(x)any(strcmp(x, extensions)), ext);
+ ext = arrayfun(@(x)x{1}, ext);
+ files = files(find(ext));
+end
+
+function extension = get_extension(file)
+[~,~,extension] = fileparts(file);

0 comments on commit d154479

Please sign in to comment.