Skip to content

Commit

Permalink
improved list_files_in_directory syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Vondrick committed Jan 27, 2012
1 parent d154479 commit 6e661d3
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions util/list_files_in_directory.m
Expand Up @@ -2,10 +2,11 @@
% Given a directory, list all files inside the directory and create a cell
% array of strings, where each string is the file location
%
% 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.
% extensions is optional and can take a variety of types:
% - if it is a cell array, only return files with extensions in this array
% - if it is the string 'images', only return images
% - if it is a string, only return files with this extension
% - if it is not given, return all files
%
% NOTE: this can be used with convert_to_I, as follows >> files =
% list_files_in_directory('~/myimages/'); >> imagesc(convert_to_I(files{10}))
Expand All @@ -23,8 +24,14 @@
files = cellfun2(@(x)[dirpath '/' x],files);

if exist('extensions', 'var'),
if ~iscell(extensions),
extensions = {'.jpg', '.png', '.gif', '.ppm', '.tif', '.jpeg'};
if isstr(extensions),
if strcmpi(extensions, 'images') || strcmpi(extensions, 'image'),
extensions = {'.jpg', '.png', '.gif', '.ppm', '.tif', '.jpeg'};
elseif strcmpi(extensions, 'videos') || strcmpi(extensions, 'video'),
extensions = {'.mp4', '.mov', '.avi', '.ogv', '.wmv'};
else,
extensions = {extensions};
end
end
ext = cellfun2(@(x)get_extension(x), files);
ext = cellfun2(@(x)any(strcmp(x, extensions)), ext);
Expand Down

0 comments on commit 6e661d3

Please sign in to comment.