annotate toolboxes/FullBNT-1.0.7/KPMtools/dirKPM.m @ 0:cc4b1211e677 tip

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