function rmpath(varargin);
%RMPATH Remove directory from search path.
%   RMPATH DIRNAME  removes the specified directory from the
%   current matlabpath.
%
%   RMPATH DIR1 DIR2 DIR3  removes all the specified directories
%   from the path.
%
%   Use the functional form of RMPATH, such as RMPATH('dir1','dir2',...),
%   when the directory specification is stored in a string.
%
%   Examples
%       rmpath c:\matlab\work
%       rmpath /home/user/matlab
%
%   See also ADDPATH, PATH, REHASH.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.19 $  $Date: 2000/06/01 01:25:41 $

% Only do case sensitive search on UNIX and VMS
casesen = isunix; 
m = [pathsep matlabpath pathsep];
for i=1:length(varargin)
  next = varargin{i};
  if ~isempty(deblank(next))
    if ~isstr(next), error('Arguments must be strings.'); end
    if ~casesen, next = lower(next); end
    if strncmp(computer,'PC',2),
      next = strrep(next,'/','\'); % Convert '/' to '\'
    end

    % Remove leading and trailing blanks.
    k = find(next ~= ' ');
    next = next(min(k):max(k)); 

    % Be robust to a trailing file separator: Check both with and
    % without the trailing filesep.
    if next(end)==filesep,
      next1 = next;
      next2 = next(1:end-1);
    else
      next1 = [next filesep];
      next2 = next;
    end

    if length(m) > length(next2)
      % Check with the trailing filesep.
      len=length(next1);
      if casesen,
        k1 = findstr([pathsep next1 pathsep],m);
      else
        k1 = findstr([pathsep next1 pathsep],lower(m));
      end
      for r=fliplr(k1),
        m(r:r+len) = [];
      end

      % Check without the trailing filesep.
      len=length(next2);
      if casesen,
        k2 = findstr([pathsep next2 pathsep],m);
      else
        k2 = findstr([pathsep next2 pathsep],lower(m));
      end
      for r=fliplr(k2),
        m(r:r+len) = [];
      end

      if isempty(k1) & isempty(k2), 
        warning(sprintf('"%s" not found in path.',next));
      end
    else
      warning(sprintf('"%s" not found in path.',next));
    end
  end
end
matlabpath(m(2:end-1))


