function addpath(varargin)
%ADDPATH Add directory to search path.
%   ADDPATH DIRNAME  prepends the specified directory to the current
%   matlabpath.  Surround the DIRNAME in quotes if the name contains a
%   space.
%   
%   ADDPATH DIR1 DIR2 DIR3 ...  prepends all the specified directories to
%   the path.
%
%   ADDPATH ... -END   appends the specified directories.
%   ADDPATH ... -BEGIN prepends the specified directories.
%
%   Use the functional form of ADDPATH, such as ADDPATH('dir1','dir2',...),
%   when the directory specification is stored in a string.
%
%   Examples
%      addpath c:\matlab\work
%      addpath 'Macintosh HD:Matlab:My Tools'
%      addpath /home/user/matlab
%
%   See also RMPATH.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.21 $  $Date: 1998/07/21 18:31:28 $

% Number of  input arguments
n = nargin;

error(nargchk(1,n,n));

% Last input argument
last = varargin{end};

% Append or prepend to the existing path
if strcmp(last,'-end') | (isequal(last,1))
  append = 1; n = n - 1;
elseif strcmp(last,'-begin') | (isequal(last,0))
  append = 0; n = n - 1;
else
  append = 0;
end

% Obtain current MATLABPATH
cpath = [matlabpath pathsep];

% Concatenate the path string, and check for duplicate paths
p = [];
for i=1:n
   next = varargin{i};
   if ~isempty(deblank(next))
     if ~isstr(next)
         error('All arguments except the last must be strings');
     end

     k = find(next ~= ' ');
     next = next(min(k):max(k)); % Remove leading and trailing blanks.
     p = [p next pathsep];
   end
end

% If p is not empty then append or prepend the new path   
if isempty(p)
   return;
elseif append
  path( path, p );
else
  path( p, path );
end

