function i = strmatch_smb(str,strs,posit,flag)
%STRMATCH Find possible matches for string.
%   I = STRMATCH(STR,STRS) looks through the rows of the character
%   array or cell array of strings STRS to find strings that begin
%   with string STR, returning the matching row indices.  STRMATCH is
%   fastest when STRS is a character array.
%
%   I = STRMATCH(STR,STRS,'exact') returns only the indices of the
%   strings in STRS matching STR exactly.
%
%   Examples
%     i = strmatch('max',strvcat('max','minimax','maximum'))
%   returns i = [1; 3] since rows 1 and 3 begin with 'max', and
%     i = strmatch('max',strvcat('max','minimax','maximum'),'exact')
%   returns i = 1, since only row 1 matches 'max' exactly.
%   
%   See also STRFIND, STRVCAT, STRCMP, STRNCMP, REGEXP.

%   Mark W. Reichelt, 8-29-94
%   Copyright 1984-2004 The MathWorks, Inc. 
%   $Revision: 1.21.4.7 $  $Date: 2004/04/16 22:08:50 $
% 
%   Revised 2006/12/13 by Seth M. Bushinsky.  Revisions includes 
%   argument "posit" which can be 'first' or 'last'.  The 'last' option
%   allows strmatch to be used to compare the end of strings or cell arrays
%   of strings to a string (str).
%   2006/12/18 - SMB - "posit" can now also be 'mid'.  'mid' searchs the
%   string array for a match with str anywhere within each string.  Case
%   sensitive.  Example- will find "ear" in "Search".

% The cell array implementation is in @cell/strmatch.m

error(nargchk(3,4,nargin));
if isempty(strs), out = []; return; end
if iscellstr(str), str = char(str); end
if iscellstr(strs), strs = char(strs); end

if ~ischar(str) || ~ischar(strs)
  error('Requires character array or cell array of strings as inputs.')
end
if (nargin==4) && ~ischar(flag)
  error('FLAG must be a string.');
end


[m,n] = size(strs);
len = numel(str);
if (nargin < 4)
    exactFlag = 0;
else
    exactFlag = 1;
end

% Special treatment for empty STR or STRS to avoid
% warnings and error below
if len==0
    str = reshape(str,1,len);
end 
if n==0
    strs = reshape(strs,max(m,1),n);
    [m,n] = size(strs);
end

if len > n
    i = [];
else
    if exactFlag && len < n % if 'exact' flag, pad str with blanks or nulls
        % Use nulls if anything in the last column is a null.
        null = char(0); 
        space = ' ';
        if ~isempty(strs) && any(strs(:,end)==null), 
            str = [str null(ones(1,n-len))];
        else
            str = [str space(ones(1,n-len))];
        end
        len = n;
    end

    if strcmp(posit, 'first'),
        mask = true(m,1);
        % walk from end of strs array and search for row starting with str.
        for outer = 1:m
            for inner = 1:len
                if (strs(outer,inner) ~= str(inner))
                    mask(outer) = false;
                    break; % exit matching this row in strs with str.
                end
            end
        end
        i = find(mask);
    elseif strcmp(posit, 'last'),
        mask = true(m,1);
        for outer = m:-1:1
            spaces = isspace(strs(outer,:)); 
            for inner = 0:len-1
                if (strs(outer,(n-sum(spaces)-inner)) ~= str(len-inner))
                    mask(outer) = false;
                    break; % exit matching this row in strs with str.
                end
            end
        end
        i = find(mask);
    elseif strcmp(posit, 'mid'),
        match = false(m,1);
        for outer = 1:m
            for inner_strs = 1:n
                inner = 1;
                count=0;
                temp = inner_strs;
                while inner<= len && temp <=n,
                    if (strs(outer,temp) == str(inner)),
                        count = count+1;
                    end
                    inner = inner+1;
                    temp = temp + 1;
                end
                inner = inner-1;
                if count == inner,
                    match(outer) = true;
                end
            end
        end
        i = find(match);
    end
end

