Skip to content

Commit 4e781d7

Browse files
committed
Updated help and demo.
1 parent e5c8150 commit 4e781d7

3 files changed

Lines changed: 125 additions & 122 deletions

File tree

getAllFiles.m

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,22 @@
3737
%
3838
% fileList = getAllFiles(rootPath, 'FileFilter', '\.m$');
3939
%
40-
% 2) Find all '.m' files in the given folder and its subfolders:
40+
% 2) Find all '.jpg', '.png', and '.tif' files:
41+
%
42+
% fileList = getAllFiles(rootPath, ...
43+
% 'FileFilter', '\.(jpg|png|tif)$');
44+
%
45+
% 3) Find all '.m' files in the given folder and its subfolders:
4146
%
4247
% fileList = getAllFiles(rootPath, 'Depth', 1, ...
4348
% 'FileFilter', '\.m$');
4449
%
45-
% 3) Find all '.m' files, returning only the file names:
50+
% 4) Find all '.m' files, returning only the file names:
4651
%
4752
% fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', ...
4853
% 'PrependPath', false);
4954
%
50-
% 4) Find all '.jpg' files with a size of more than 1MB:
55+
% 5) Find all '.jpg' files with a size of more than 1MB:
5156
%
5257
% bigFcn = @(s) (s.bytes > 1024^2);
5358
% fileList = getAllFiles(rootPath, 'FileFilter', '\.jpg$', ...
@@ -57,7 +62,7 @@
5762

5863
% Author: Ken Eaton
5964
% Version: MATLAB R2016b
60-
% Last modified: 2/23/17
65+
% Last modified: 3/6/17
6166
% Copyright 2017 by Kenneth P. Eaton
6267
%--------------------------------------------------------------------------
6368

getAllFiles_demo.m

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,81 @@
22
% The _getAllFiles_ function recursively collects a list of files from a
33
% folder tree, allowing you to specify the selection criteria for which
44
% files are collected and how they are formatted. For these examples, we'll
5-
% be using the Image Processing Toolbox path.
5+
% be using the main MATLAB toolbox path.
66
%%
77

8-
rootPath = 'C:\Program Files\MATLAB\R2016b\toolbox\images';
8+
rootPath = 'C:\Program Files\MATLAB\R2016b\toolbox\matlab';
9+
format compact
910

1011
%% The 'FileFilter' option
11-
% We can specify a regular-expression pattern to filter the file names on,
12-
% collecting a list of files that match. Here's an example that uses the
13-
% 'FileFilter' option to recursively find every file with a '.m' extension
14-
% in the Image Processing Toolbox:
12+
% We can specify a
13+
% <https://www.mathworks.com/help/matlab/matlab_prog/regular-expressions.html
14+
% regular-expression> pattern to filter the file names on, collecting a
15+
% list of files that match. Here's an example that uses the 'FileFilter'
16+
% option to recursively find every file with a '.m' extension:
1517

1618
fileList = getAllFiles(rootPath, 'FileFilter', '\.m$');
17-
size(fileList)
18-
display(fileList(1:5))
19+
fprintf('%d files found.\n', size(fileList, 1));
20+
fprintf('%s\n', fileList{1:5}, '...');
1921

2022
%%
2123
% It's a pretty long list, so I've only shown the first 5 files it finds.
2224
% Notice they are listed with the full path prepended by default.
2325

26+
%%
27+
% If you have multiple match expressions to filter on, you can use
28+
% <https://www.mathworks.com/help/matlab/matlab_prog/regular-expressions.html#f0-42884
29+
% grouping operators> to include them all in one expression. For example,
30+
% this will find every '.jpg', '.bmp', and '.tif' file:
31+
32+
fileList = getAllFiles(rootPath, 'FileFilter', '\.(jpg|bmp|tif)$');
33+
fprintf('%d files found.\n', size(fileList, 1));
34+
fprintf('%s\n', fileList{1:5}, '...');
35+
2436
%% The 'Depth' option
2537
% If we don't want to search quite so far down the folder tree, we can
2638
% limit the search depth with the 'Depth' option. Let's see how many '.m'
27-
% files are in the root folder for the Image Processing Toolbox:
39+
% files are in the root folder:
2840

2941
fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', 'Depth', 0);
30-
size(fileList)
42+
fprintf('%d files found.\n', size(fileList, 1));
3143

3244
%%
3345
% Looks like none are. They are all contained in subfolders. Let's see how
3446
% many are located in just the immediate subfolders of the root folder:
3547

3648
fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', 'Depth', 1);
37-
size(fileList)
49+
fprintf('%d files found.\n', size(fileList, 1));
3850

3951
%% The 'PrependPath' option
4052
% Maybe we just want the file names, but don't care about the absolute
4153
% paths. In this case, we just set the 'PrependPath' option to _false_:
4254

4355
fileList = getAllFiles(rootPath, 'FileFilter', '\.m$', ...
4456
'PrependPath', false);
45-
display(fileList(1:5))
57+
fprintf('%d files found.\n', size(fileList, 1));
58+
fprintf('%s\n', fileList{1:5}, '...');
4659

4760
%% The 'ValidateFcn' option
4861
% Sometimes we might want to select files based on a more complicated
4962
% criteria than just what's in their names. In this case, we can use the
5063
% 'ValidateFcn' option to specify a function that is to be run on each file
5164
% found. This function should accept as input a structure of the form
52-
% returned by the _dir_ function and return a logical value (_true_ to
53-
% collect it in the list, _false_ to ignore it). First, let's find all the
54-
% '.jpg' files in the Image Processing Toolbox:
65+
% returned by the |dir| function and return a logical value (|true| to
66+
% collect it in the list, |false| to ignore it). First, let's find all the
67+
% '.png' files:
5568

56-
fileList = getAllFiles(rootPath, 'FileFilter', '\.jpg$');
57-
display(fileList)
69+
fileList = getAllFiles(rootPath, 'FileFilter', '\.png$');
70+
fprintf('%d files found.\n', size(fileList, 1));
5871

5972
%%
6073
% Now, we can specify an anonymous function that gets the byte size of each
61-
% file and returns _true_ for only those greater than 1MB:
74+
% file and returns |true| for only those greater than 250KB:
6275

63-
bigFcn = @(s) (s.bytes > 1024^2);
64-
fileList = getAllFiles(rootPath, 'FileFilter', '\.jpg$', ...
76+
bigFcn = @(s) (s.bytes > 512^2);
77+
fileList = getAllFiles(rootPath, 'FileFilter', '\.png$', ...
6578
'ValidateFcn', bigFcn);
66-
display(fileList)
79+
fprintf('%s\n', fileList{:});
6780

6881
%%
69-
% Just the two.
82+
% Just the one.

0 commit comments

Comments
 (0)