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