function [index,status] = pmatch(StrCell,str)
%PMATCH  Case insensitive string matching with automatic completion.
%
%   [INDICES,STATUS] = PMATCH(STRCELL,STR)  takes a cell array 
%   STRCELL containing reference names and a cell array STR of 
%   strings to be matched.  It returns a vector of (absolute) 
%   INDICES such that  INDICES(I) = J  if STR{I} matches the 
%   reference name STRCELL{J}.
%
%   STATUS is the empty matrix if exactly one cell matches each
%   string.  Otherwise it returns a "standard" error message.
%
%   Note:  PMATCH is optimized for the LTI object and uses only
%   the first two characters of STR for comparison purposes.

%   Author(s): A. Potvin, 3-1-94
%   Revised: P. Gahinet, 4-1-96
%   Copyright (c) 1986-97 by The MathWorks, Inc.
%   $Revision: 1.3 $

%   Note: replace nchars = min(l,2) by nchars = l below to obtain 
%   following general behavior:
%        StrCell = {'CurrentMenu'; 'Color'; 'CurrentObject'}
%        STR = 'current' 
%           INDICES = []
%           STATUS  = 'Ambiguous property name: supply more characters'
%        STR = 'currentmenu' 
%           INDICES = 1
%           STATUS  = []
%        STR = 'foobar' 
%           INDICES = []
%           STATUS  = 'Invalid object property'


if ndims(str)>2 | min(size(str))>1,
   status = 'Property names must be single-line strings';
   index = [];
   return
end

if isstr(str),

   % STR is a single-line string
   index = [];
   str = str(:)';
   s = lower(str);
   l = length(s);
   if l==0,
     status = 'Ambiguous property name: supply at least one character';
     return
   end

   % Set number of chars used to identify name
   nchars = min(l,2);  
   window = 1:nchars;

   % Find all matches in StrCell
   for i=1:length(StrCell),
      tmp = lower(StrCell{i});
      % Following relies on shortcut logic operators
      if length(tmp)>=nchars & tmp(window)==s(window),
         index = [index; i];
      end
   end
   lindex = length(index);
   switch lindex
     case 0,     status = ['Invalid object property "' str '"'];
     case 1,     status = [];
     otherwise,  status = ['Ambiguous property name "' str ...
                                            '": supply more characters'];
                 index = [];
   end

elseif iscellstr(str),

   % STR is a list of strings
   l = length(str);
   ind = zeros(1,l);
   
   for i=1:l,  
      [index(i),status] = pmatch(StrCell,str{i});
      if ~isempty(status), 
         index = [];  return
      end
   end

else

   % Unexpected type for STR
   index = []; 
   status = 'Property names must be strings';

end
   

% end pmatch.m
