annotate toolboxes/FullBNT-1.0.7/KPMtools/dirKPM.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 filenames = dirKPM(dirname, ext, varargin)
wolffd@0 2 % dirKPM Like the built-in dir command, but returns filenames as a cell array instead of a struct
wolffd@0 3 %
wolffd@0 4 % filenames = dirKPM(dirname)
wolffd@0 5 % returns all files, except '.' and '..'
wolffd@0 6 %
wolffd@0 7 % filenames = dirKPM('images', '*.jpg')
wolffd@0 8 % returns files with this extension
wolffd@0 9 % eg filenames{1} = 'foo.jpg' etc
wolffd@0 10 %
wolffd@0 11 % OPTIONAL ARGUMENTS [default in brackets]
wolffd@0 12 % filenames = dirKPM('images', '', param1, val1, param2, val2, ...)
wolffd@0 13 %
wolffd@0 14 % 'fileType'='image' ['all'] means return files with extension .jpg, .png, .bmp
wolffd@0 15 %
wolffd@0 16 % 'prepend'=1 [0] means preprend folder name to filename
wolffd@0 17 % eg filenames{1} = 'images/foo.jpg'
wolffd@0 18 %
wolffd@0 19 % 'doSort'=1 [1] means sort filenames in ascending alphanumerical order (where possible)
wolffd@0 20 %
wolffd@0 21 % 'doRecurse'=1 [0] recursive dir, apply the same dirKPM call on all
wolffd@0 22 % subfolders (decrease MAXDEPTH option to prevent recursion from branching
wolffd@0 23 % too explosively)
wolffd@0 24
wolffd@0 25 if nargin < 1, dirname = '.'; end
wolffd@0 26
wolffd@0 27 if nargin < 2, ext = ''; end
wolffd@0 28
wolffd@0 29 [fileType, prepend, doSort, doRecurse, MAXDEPTH, DEPTH] = process_options(...
wolffd@0 30 varargin, 'fileType', 'all', 'prepend', 0, 'doSort', 1, 'doRecurse', 0,...
wolffd@0 31 'MAXDEPTH', 3, 'DEPTH', 0);
wolffd@0 32
wolffd@0 33 tmp = dir(fullfile(dirname, ext));
wolffd@0 34 [filenames I] = setdiff({tmp.name}, {'.', '..'});
wolffd@0 35 tmp = tmp(I);
wolffd@0 36
wolffd@0 37 if doRecurse && sum([tmp.isdir])>0 && DEPTH<MAXDEPTH
wolffd@0 38 for fi=1:length(tmp)
wolffd@0 39 subDirFilenames = {};
wolffd@0 40
wolffd@0 41 if tmp(fi).isdir
wolffd@0 42 varargin = change_option( varargin, 'prepend', false );
wolffd@0 43 varargin = change_option( varargin, 'doSort', false );
wolffd@0 44 varargin = change_option( varargin, 'DEPTH', DEPTH+1 );
wolffd@0 45 subDirFilenames = dirKPM( fullfile(dirname,tmp(fi).name), ext, varargin{:} );
wolffd@0 46
wolffd@0 47 for sdfi=1:length(subDirFilenames)
wolffd@0 48 subDirFilenames{sdfi} = fullfile(tmp(fi).name, subDirFilenames{sdfi});
wolffd@0 49 end
wolffd@0 50 end
wolffd@0 51
wolffd@0 52
wolffd@0 53 nfilenames = length(filenames);
wolffd@0 54 if length(subDirFilenames)>0
wolffd@0 55 filenames(nfilenames+1:nfilenames+length(subDirFilenames)) = subDirFilenames;
wolffd@0 56 end
wolffd@0 57 end
wolffd@0 58 end
wolffd@0 59
wolffd@0 60 nfiles = length(filenames);
wolffd@0 61 if nfiles==0 return; end
wolffd@0 62
wolffd@0 63 switch fileType
wolffd@0 64 case 'image',
wolffd@0 65 for fi=1:nfiles
wolffd@0 66 good(fi) = isImage(filenames{fi});
wolffd@0 67 end
wolffd@0 68 filenames = filenames(find(good));
wolffd@0 69 case 'all',
wolffd@0 70 % no-op
wolffd@0 71 otherwise
wolffd@0 72 error(sprintf('unrecognized file type %s', fileType));
wolffd@0 73 end
wolffd@0 74
wolffd@0 75 if doSort
wolffd@0 76 % % sort filenames alphanumerically (if possible)
wolffd@0 77 % DJE, buggy, MUST save tmp.anr/snr/str or else we potentially lose
wolffd@0 78 % filenames
wolffd@0 79 % tmp = asort(filenames, '-s', 'ascend');
wolffd@0 80 % if ~isempty(tmp.anr)
wolffd@0 81 % filenames = tmp.anr';
wolffd@0 82 % else
wolffd@0 83 % filenames = tmp.str';
wolffd@0 84 % end
wolffd@0 85 % if names could not be sorted, return original order
wolffd@0 86
wolffd@0 87 filenames=sort(filenames);
wolffd@0 88
wolffd@0 89 end
wolffd@0 90
wolffd@0 91
wolffd@0 92 if prepend
wolffd@0 93 nfiles = length(filenames);
wolffd@0 94 for fi=1:nfiles
wolffd@0 95 filenames{fi} = fullfile(dirname, filenames{fi});
wolffd@0 96 end
wolffd@0 97 end
wolffd@0 98