-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookforfiles.m
60 lines (50 loc) · 1.46 KB
/
lookforfiles.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
% LOOKFORFILES.M
% function fileNames = lookforfiles(filedir,filtInclude,filtstart,filtext)
% Get the name of all files from a directory matching a desired pattern.
function fileNames = lookforfiles(filedir,filtInclude,filtstart,filtext)
% Interpret the inputs %
if ischar(filtInclude)
filtInclude = {filtInclude};
end;
nFilt = length(filtInclude);
if isempty(filtext)
filtext = '*.*';
elseif ~strcmp(filtext(1),'*')
warning('Input argument ''filtext'' should start with "*" if it represents a trailing string.');
end;
% First get all the file names %
olddir = pwd; cd(filedir);
fileNamesAll = dir(filtext);
fileNamesAll = {fileNamesAll.name};
cd(olddir);
if isempty(fileNamesAll)
fileNames = {''};
return;
end;
nFiles = length(fileNamesAll);
% Then filter based on the start and include strings %
if ~isempty(filtstart)
matchTemp = regexpi(fileNamesAll,filtstart);
matchstart = false(1,nFiles);
for i = 1:length(matchTemp)
if ~isempty(matchTemp{i}) && matchTemp{i}(1)==1
matchstart(i) = true;
end;
end;
else
matchstart = true(1,nFiles);
end;
if nFilt
MatchStart = false(nFilt,nFiles);
for ff = 1:nFilt
matchTemp = regexpi(fileNamesAll,filtInclude{ff});
for i = 1:length(matchTemp)
if matchTemp{i}>=1, MatchStart(ff,i) = true; end;
end;
end;
matchinclude = all(MatchStart,1);
else
matchinclude = true(1,nFiles);
end;
matchfilt = matchstart & matchinclude;
fileNames = fileNamesAll(matchfilt);