annotate toolboxes/bioakustik_tools/filesys/forAllFilesInDir.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function y = forAllFilesInDir(startdir, filetypes, func, recursive, y, varargin)
wolffd@0 2 % y = forAllFilesInDir(startdir, filetypes, func, recursive, y, varargin)
wolffd@0 3 %
wolffd@0 4 % Traverse directory structure, performing for each file of a specified filetype
wolffd@0 5 % an action defined by a function handle.
wolffd@0 6 %
wolffd@0 7 % INPUT:
wolffd@0 8 % ------
wolffd@0 9 % startdir ..... (string) Start directory.
wolffd@0 10 % Any directory containing a file named "skip" will be skipped.
wolffd@0 11 % filetypes .... (cell array of strings) File type filter, e.g. {'*.amc' '*.asf'}
wolffd@0 12 % or {'*.*'} for all files
wolffd@0 13 % func ......... (function handle) Worker function to be called for each file
wolffd@0 14 % The function designated by func will be passed the current complete
wolffd@0 15 % filename (including pathname) as the first argument. If the parameter list
wolffd@0 16 % varargin is nonempty, a single cell array with the current filename as the
wolffd@0 17 % first entry followed by the remaining parameters in varargin will be passed.
wolffd@0 18 % recursive .... (boolean) Flag indicating whether recursive traversal is desired or not.
wolffd@0 19 % y ............ (matrix or cell array) Initial value for output container. Usually empty ([]).
wolffd@0 20 % varargin ..... (cell array of parameters) Parameters to be passed to func, see "func".
wolffd@0 21 %
wolffd@0 22 % OUTPUT:
wolffd@0 23 % -------
wolffd@0 24 % y ............ (matrix or cell array) Collected outputs of calls to func, vertically concatenated.
wolffd@0 25 %
wolffd@0 26 % EXAMPLE:
wolffd@0 27 % --------
wolffd@0 28 % y = forAllFilesInDir('S:\data_MoCap\MoCaDaDB\AMC',{'*.amc'},@getAMCLengthInSeconds,true,[]);
wolffd@0 29 % y = forAllFilesInDir('S:\data_MoCap\MoCaDaDB\AMC',{'*.amc'},@getAMCLengthInSeconds,true,[],param1,...,paramn);
wolffd@0 30
wolffd@0 31 if (startdir(end)~='\')
wolffd@0 32 startdir = [startdir '\'];
wolffd@0 33 end
wolffd@0 34
wolffd@0 35 files = dir(startdir);
wolffd@0 36 if (isempty(strmatch('skip',{files.name},'exact'))) % only process dir if no "skip" file is present!
wolffd@0 37 disp(['Entered directory ' startdir]);
wolffd@0 38 files = [];
wolffd@0 39 for k=1:length(filetypes)
wolffd@0 40 fls = dir([startdir filetypes{k}]);
wolffd@0 41 files = [files; fls];
wolffd@0 42 end
wolffd@0 43
wolffd@0 44 for k=1:length(files)
wolffd@0 45 disp(['Processing file ' files(k).name]);
wolffd@0 46 if (nargin>5)
wolffd@0 47 args = horzcat({startdir files(k).name}, varargin); % changed by -fk to pass both dir + filename
wolffd@0 48 else
wolffd@0 49 args = {startdir files(k).name}; % changed by -fk to pass both dir + filename
wolffd@0 50 end
wolffd@0 51 a = feval(func,args{:});
wolffd@0 52 y = [y; a];
wolffd@0 53 end
wolffd@0 54
wolffd@0 55 if (recursive)
wolffd@0 56 dirs = dir(startdir);
wolffd@0 57 isdir = cell2mat({dirs.isdir});
wolffd@0 58 dirs = dirs(find(isdir));
wolffd@0 59 for k = 1:length(dirs)
wolffd@0 60 if (~strcmp(dirs(k).name,'.') & ~strcmp(dirs(k).name,'..'))
wolffd@0 61 y = forAllFilesInDir([startdir dirs(k).name], filetypes, func, recursive, y, varargin{:});
wolffd@0 62 end
wolffd@0 63 end
wolffd@0 64 end
wolffd@0 65 disp(['Left directory ' startdir]);
wolffd@0 66 else
wolffd@0 67 disp(['Skipped directory ' startdir]);
wolffd@0 68 end