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\t oolbox\images' ;
8+ rootPath = ' C:\Program Files\MATLAB\R2016b\t oolbox\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
1618fileList = 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
2941fileList = 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
3648fileList = 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
4355fileList = 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