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.
%   ADDPATH ... -FROZEN disables directory change detection for directories
%                       being added and thereby conserves Windows change
%                       notification resources (Windows only).
%
%   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 /home/user/matlab
%
%   See also RMPATH, PATH, GENPATH, REHASH.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.27 $  $Date: 2000/09/14 14:01:18 $

% Number of  input arguments
n = nargin;

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

% Last input argument
last = varargin{end};

append = 0;
freeze = 0;

% 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;
elseif strcmp(last,'-frozen')  & strncmp(computer,'PC',2)
  freeze = 1; n = n - 1;
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 empty then return   
if isempty(p)
  return;
end

unfreeze = 0;

% See whether frozen is desired, where the state is not already set frozen
if freeze
  oldfreeze = system_dependent('DirsAddedFreeze');
  % Check whether old unfrozen state needs to be restored
  if ~isempty(findstr(oldfreeze,'unfrozen'))
    unfreeze = 1;
  end
end

% Append or prepend the new path   
try
  err = 0;
  if append
    path( path, p );
  else
    path( p, path );
  end
catch
  err = 1;
end

if unfreeze
  system_dependent('DirsAddedUnfreeze');
end




