-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfindFile.m
67 lines (59 loc) · 1.76 KB
/
findFile.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
61
62
63
64
65
66
67
function [file, idx, pid] = findFile(files, searchStr, exact)
% main workhorse file for searching for strings
% [file, idx, pid] = findFile(files, searchStr)
% gets the index for a file in [files] that matches a search string
% inputs:
% files
% [cell array] of strings to compare searchStr to
% [string] path to directory
% searchStr
% [string] string to compare <files> to
% [cell-array] of strings to compare
% exact [boolean] flag for wheter searchStr has to match exactly
% outputs:
% file
% [cell-array] of strings that gives the file names that match
% searchStr. If searchStr is a cell-array, file will
% return the files that match all of the searchStr
% values
% idx
% [double] vector of indices into files
% pid
% [cell-array] of the char index for files
% 2015 jly merged Jake and Leor's findFile versions
if ~exist('exact', 'var')
exact = false;
end
if ~iscell(files) && isdir(files)
files = dir(files);
files = {files(:).name};
files = files(:);
end
if nargin < 2 || isempty(searchStr)
searchStr = '.mat';
end
% enforce search str to be a cell array
if ischar(searchStr)
searchStr = {searchStr};
end
nStr = numel(searchStr);
% for loop is faster than cellfun
nFiles = numel(files);
pid = cell(nFiles,nStr);
if exact
for kFile = 1:nFiles
for kStr = 1:nStr
tmp = strcmp(files{kFile}, searchStr{kStr});
tmp(tmp==0)=[];
pid{kFile, kStr} = tmp;
end
end
else
for kFile = 1:nFiles
for kStr = 1:nStr
pid{kFile,kStr} = strfind(files{kFile}, searchStr{kStr});
end
end
end
idx = find(all(~cellfun(@isempty, pid),2));
file = files(idx);