time | calls | line |
---|
| | 1 | function i = strmatch(str,strs,flag)
|
| | 2 | %STRMATCH Find possible matches for string.
|
| | 3 | % I = STRMATCH(STR, STRARRAY) looks through the rows of the character
|
| | 4 | % array or cell array of strings STRARRAY to find strings that begin
|
| | 5 | % with the string contained in STR, and returns the matching row indices.
|
| | 6 | % Any trailing space characters in STR or STRARRAY are ignored when
|
| | 7 | % matching. STRMATCH is fastest when STRARRAY is a character array.
|
| | 8 | %
|
| | 9 | % I = STRMATCH(STR, STRARRAY, 'exact') compares STR with each row of
|
| | 10 | % STRARRAY, looking for an exact match of the entire strings. Any
|
| | 11 | % trailing space characters in STR or STRARRAY are ignored when matching.
|
| | 12 | %
|
| | 13 | % Examples
|
| | 14 | % i = strmatch('max',strvcat('max','minimax','maximum'))
|
| | 15 | % returns i = [1; 3] since rows 1 and 3 begin with 'max', and
|
| | 16 | % i = strmatch('max',strvcat('max','minimax','maximum'),'exact')
|
| | 17 | % returns i = 1, since only row 1 matches 'max' exactly.
|
| | 18 | %
|
| | 19 | % STRMATCH is not recommended. Use STRNCMP instead.
|
| | 20 | %
|
| | 21 | % See also STRFIND, STRVCAT, STRCMP, STRNCMP, REGEXP.
|
| | 22 |
|
| | 23 | % Mark W. Reichelt, 8-29-94
|
| | 24 | % Copyright 1984-2010 The MathWorks, Inc.
|
| | 25 |
|
| | 26 | % The cell array implementation is in @cell/strmatch.m
|
| | 27 |
|
| 87 | 28 | narginchk(2,3);
|
| | 29 |
|
| 87 | 30 | [m,n] = size(strs);
|
| 87 | 31 | len = numel(str);
|
| | 32 |
|
| 87 | 33 | if (nargin==3)
|
| 3 | 34 | exactMatch = true;
|
| 3 | 35 | if ~ischar(flag)
|
| | 36 | warning(message('MATLAB:strmatch:InvalidFlagType'));
|
| 3 | 37 | elseif ~strcmpi(flag,'exact')
|
| | 38 | warning(message('MATLAB:strmatch:InvalidFlag', flag, flag));
|
| | 39 | end
|
| 84 | 40 | else
|
| 84 | 41 | exactMatch = false;
|
| 84 | 42 | end
|
| | 43 |
|
| | 44 | % Special treatment for empty STR or STRS to avoid
|
| | 45 | % warnings and error below
|
| 87 | 46 | if len==0
|
| | 47 | str = reshape(str,1,len);
|
| | 48 | end
|
| 87 | 49 | if n==0
|
| | 50 | strs = reshape(strs,max(m,1),n);
|
| | 51 | [m,n] = size(strs);
|
| | 52 | end
|
| | 53 |
|
| 87 | 54 | if len > n
|
| 3 | 55 | i = [];
|
| 84 | 56 | else
|
| 84 | 57 | if exactMatch && len < n % if 'exact' flag, pad str with blanks or nulls
|
| 3 | 58 | [~,strn] = size(str);
|
| 3 | 59 | if strn ~= len
|
| | 60 | error(message('MATLAB:strmatch:InvalidShape'));
|
| 3 | 61 | else
|
| | 62 | % Use nulls if anything in the last column is a null.
|
| 3 | 63 | null = char(0);
|
| 3 | 64 | space = ' ';
|
| 3 | 65 | if ~isempty(strs) && any(strs(:,end)==null),
|
| | 66 | str = [str null(ones(1,n-len))];
|
| 3 | 67 | else
|
| 3 | 68 | str = [str space(ones(1,n-len))];
|
| 3 | 69 | end
|
| 3 | 70 | len = n;
|
| 3 | 71 | end
|
| 3 | 72 | end
|
| | 73 |
|
| 84 | 74 | mask = true(m,1);
|
| | 75 | % walk from end of strs array and search for row starting with str.
|
| 84 | 76 | for outer = 1:m
|
0.02 | 4770 | 77 | for inner = 1:len
|
| 6368 | 78 | if (strs(outer,inner) ~= str(inner))
|
0.01 | 4674 | 79 | mask(outer) = false;
|
0.01 | 4674 | 80 | break; % exit matching this row in strs with str.
|
| | 81 | end
|
| 1694 | 82 | end
|
| 4770 | 83 | end
|
| 84 | 84 | i = find(mask);
|
| 84 | 85 | end
|