wolffd@0: function y = forAllFilesInDir(startdir, filetypes, func, recursive, y, varargin) wolffd@0: % y = forAllFilesInDir(startdir, filetypes, func, recursive, y, varargin) wolffd@0: % wolffd@0: % Traverse directory structure, performing for each file of a specified filetype wolffd@0: % an action defined by a function handle. wolffd@0: % wolffd@0: % INPUT: wolffd@0: % ------ wolffd@0: % startdir ..... (string) Start directory. wolffd@0: % Any directory containing a file named "skip" will be skipped. wolffd@0: % filetypes .... (cell array of strings) File type filter, e.g. {'*.amc' '*.asf'} wolffd@0: % or {'*.*'} for all files wolffd@0: % func ......... (function handle) Worker function to be called for each file wolffd@0: % The function designated by func will be passed the current complete wolffd@0: % filename (including pathname) as the first argument. If the parameter list wolffd@0: % varargin is nonempty, a single cell array with the current filename as the wolffd@0: % first entry followed by the remaining parameters in varargin will be passed. wolffd@0: % recursive .... (boolean) Flag indicating whether recursive traversal is desired or not. wolffd@0: % y ............ (matrix or cell array) Initial value for output container. Usually empty ([]). wolffd@0: % varargin ..... (cell array of parameters) Parameters to be passed to func, see "func". wolffd@0: % wolffd@0: % OUTPUT: wolffd@0: % ------- wolffd@0: % y ............ (matrix or cell array) Collected outputs of calls to func, vertically concatenated. wolffd@0: % wolffd@0: % EXAMPLE: wolffd@0: % -------- wolffd@0: % y = forAllFilesInDir('S:\data_MoCap\MoCaDaDB\AMC',{'*.amc'},@getAMCLengthInSeconds,true,[]); wolffd@0: % y = forAllFilesInDir('S:\data_MoCap\MoCaDaDB\AMC',{'*.amc'},@getAMCLengthInSeconds,true,[],param1,...,paramn); wolffd@0: wolffd@0: if (startdir(end)~='\') wolffd@0: startdir = [startdir '\']; wolffd@0: end wolffd@0: wolffd@0: files = dir(startdir); wolffd@0: if (isempty(strmatch('skip',{files.name},'exact'))) % only process dir if no "skip" file is present! wolffd@0: disp(['Entered directory ' startdir]); wolffd@0: files = []; wolffd@0: for k=1:length(filetypes) wolffd@0: fls = dir([startdir filetypes{k}]); wolffd@0: files = [files; fls]; wolffd@0: end wolffd@0: wolffd@0: for k=1:length(files) wolffd@0: disp(['Processing file ' files(k).name]); wolffd@0: if (nargin>5) wolffd@0: args = horzcat({startdir files(k).name}, varargin); % changed by -fk to pass both dir + filename wolffd@0: else wolffd@0: args = {startdir files(k).name}; % changed by -fk to pass both dir + filename wolffd@0: end wolffd@0: a = feval(func,args{:}); wolffd@0: y = [y; a]; wolffd@0: end wolffd@0: wolffd@0: if (recursive) wolffd@0: dirs = dir(startdir); wolffd@0: isdir = cell2mat({dirs.isdir}); wolffd@0: dirs = dirs(find(isdir)); wolffd@0: for k = 1:length(dirs) wolffd@0: if (~strcmp(dirs(k).name,'.') & ~strcmp(dirs(k).name,'..')) wolffd@0: y = forAllFilesInDir([startdir dirs(k).name], filetypes, func, recursive, y, varargin{:}); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: disp(['Left directory ' startdir]); wolffd@0: else wolffd@0: disp(['Skipped directory ' startdir]); wolffd@0: end