


LOCATE Locate one or more files in the search path.
LOCATE FILEGLOB prints a list of all MATLAB files on the path that match
FILEGLOB. Private subdirectories are also searched.
LIST = LOCATE(FILEGLOB) returns a list (cell array) of the files. The list
is not displayed.
Multiple filegobs may be given, e.g., LOCATE('*.m', '*.dll').
Note that the globbing is done by MATLAB and not by the operating system.
Examples:
locate *plot.m - Find all files ending with 'plot.m'.
locate im* - Find all files starting with 'im'.
See also DIR, SYSGLOB.

0001 function locations = locate(varargin) 0002 %LOCATE Locate one or more files in the search path. 0003 % 0004 % LOCATE FILEGLOB prints a list of all MATLAB files on the path that match 0005 % FILEGLOB. Private subdirectories are also searched. 0006 % 0007 % LIST = LOCATE(FILEGLOB) returns a list (cell array) of the files. The list 0008 % is not displayed. 0009 % 0010 % Multiple filegobs may be given, e.g., LOCATE('*.m', '*.dll'). 0011 % 0012 % Note that the globbing is done by MATLAB and not by the operating system. 0013 % 0014 % Examples: 0015 % 0016 % locate *plot.m - Find all files ending with 'plot.m'. 0017 % locate im* - Find all files starting with 'im'. 0018 % 0019 % See also DIR, SYSGLOB. 0020 0021 % Author: Peter J. Acklam 0022 % Time-stamp: 2003-10-13 13:06:17 +0200 0023 % E-mail: pjacklam@online.no 0024 % URL: http://home.online.no/~pjacklam 0025 0026 % check number of input arguments 0027 nargsin = nargin; 0028 error(nargchk(1, Inf, nargsin)); 0029 0030 dirs = path2cell; % convert path to list 0031 startdir = cd; % get starting directory 0032 0033 display = 1; 0034 if nargout % if there are output arguments 0035 locations = {}; % initialize output list 0036 display = 0; % and don't display results 0037 end 0038 0039 while length(dirs) % while there are unprocessed dirs... 0040 0041 directory = dirs{1}; % get first directory 0042 dirs = dirs(2:end); % and remove it from the list 0043 0044 % fprintf('%s\n', directory); 0045 cd(directory); % chdir to the directory 0046 0047 % get the list of file names that match the glob(s) 0048 found = {}; 0049 for i = 1:nargsin 0050 % dirinfo = dir(fullfile(directory,varargin{i})); 0051 dirinfo = dir(varargin{i}); 0052 found = { found{:} dirinfo.name }; 0053 end 0054 0055 cd(startdir); % chdir back to starting directory 0056 0057 % Append list of files found in this directory to the main list or 0058 % display the files if no output is wanted. 0059 if ~isempty(found) 0060 % get unique list of files 0061 found = unique(found); 0062 0063 % prepend directory name 0064 for i = 1:length(found) 0065 found{i} = fullfile(directory, found{i}); 0066 end 0067 if nargout 0068 locations = [ locations(:) ; found(:) ]; 0069 else 0070 fprintf('%s\n', found{:}); 0071 end 0072 end 0073 0074 % If this directory has a private subdirectory, look there too. 0075 subdirectory = fullfile(directory, 'private'); 0076 if exist(subdirectory, 'dir') 0077 dirs = { subdirectory , dirs{:} }'; 0078 end 0079 0080 end