1 |
function locations = locate(varargin) |
---|
2 |
%LOCATE Locate one or more files in the search path. |
---|
3 |
% |
---|
4 |
% LOCATE FILEGLOB prints a list of all MATLAB files on the path that |
---|
5 |
% match FILEGLOB. Private subdirectories are also searched. |
---|
6 |
% |
---|
7 |
% LIST = LOCATE(FILEGLOB) returns a list (cell array) of the files. The |
---|
8 |
% list is not displayed. |
---|
9 |
% |
---|
10 |
% Multiple filegobs may be given, e.g., LOCATE('*.m', '*.dll'). |
---|
11 |
% |
---|
12 |
% Note that the globbing is done by MATLAB and not by the operating |
---|
13 |
% system. |
---|
14 |
% |
---|
15 |
% Examples: |
---|
16 |
% |
---|
17 |
% locate *plot.m - Find all files ending with 'plot.m'. |
---|
18 |
% locate im* - Find all files starting with 'im'. |
---|
19 |
% |
---|
20 |
% See also DIR, SYSGLOB. |
---|
21 |
|
---|
22 |
% Author: Peter J. Acklam |
---|
23 |
% Time-stamp: 2003-03-31 18:21:30 +0200 |
---|
24 |
% E-mail: pjacklam@online.no |
---|
25 |
% URL: http://home.online.no/~pjacklam |
---|
26 |
|
---|
27 |
% check number of input arguments |
---|
28 |
nargsin = nargin; |
---|
29 |
error(nargchk(1, Inf, nargsin)); |
---|
30 |
|
---|
31 |
dirs = path2cell; % convert path to list |
---|
32 |
startdir = cd; % get starting directory |
---|
33 |
|
---|
34 |
display = 1; |
---|
35 |
if nargout % if there are output arguments |
---|
36 |
locations = {}; % initialize output list |
---|
37 |
display = 0; % and don't display results |
---|
38 |
end |
---|
39 |
|
---|
40 |
while length(dirs) % while there are unprocessed dirs... |
---|
41 |
|
---|
42 |
directory = dirs{1}; % get first directory |
---|
43 |
dirs = dirs(2:end); % and remove it from the list |
---|
44 |
|
---|
45 |
% fprintf('%s\n', directory); |
---|
46 |
cd(directory); % chdir to the directory |
---|
47 |
|
---|
48 |
% get the list of file names that match the glob(s) |
---|
49 |
found = {}; |
---|
50 |
for i = 1:nargsin |
---|
51 |
% dirinfo = dir(fullfile(directory,varargin{i})); |
---|
52 |
dirinfo = dir(varargin{i}); |
---|
53 |
found = { found{:} dirinfo.name }; |
---|
54 |
end |
---|
55 |
|
---|
56 |
cd(startdir); % chdir back to starting directory |
---|
57 |
|
---|
58 |
% Append list of files found in this directory to the main list or |
---|
59 |
% display the files if no output is wanted. |
---|
60 |
if ~isempty(found) |
---|
61 |
% get unique list of files |
---|
62 |
found = unique(found); |
---|
63 |
|
---|
64 |
% prepend directory name |
---|
65 |
for i = 1:length(found) |
---|
66 |
found{i} = fullfile(directory, found{i}); |
---|
67 |
end |
---|
68 |
if nargout |
---|
69 |
locations = [ locations(:) ; found(:) ]; |
---|
70 |
else |
---|
71 |
fprintf('%s\n', found{:}); |
---|
72 |
end |
---|
73 |
end |
---|
74 |
|
---|
75 |
% If this directory has a private subdirectory, look there too. |
---|
76 |
subdirectory = fullfile(directory, 'private'); |
---|
77 |
if exist(subdirectory, 'dir') |
---|
78 |
dirs = { subdirectory , dirs{:} }'; |
---|
79 |
end |
---|
80 |
|
---|
81 |
end |
---|