time | Calls | line |
---|
| | 1 | function i = strmatch(str,strs,flag)
|
| | 2 | %STRMATCH Find possible matches for character arrays or strings.
|
| | 3 | % STRMATCH is not recommended. Use STARTSWITH or VALIDATESTRING instead.
|
| | 4 | %
|
| | 5 | % IDX = STRMATCH(STR, STRARRAY) looks through the rows of the character array,
|
| | 6 | % string array, or cell array of character vectors STRARRAY to find a
|
| | 7 | % character vector that begins with STR, and returns the matching row
|
| | 8 | % indices. Any trailing space characters in STR or STRARRAY are ignored
|
| | 9 | % when matching. STRMATCH is fastest when STRARRAY is a character array.
|
| | 10 | %
|
| | 11 | % IDX = STRMATCH(STR, STRARRAY, 'exact') compares STR with each row of
|
| | 12 | % STRARRAY, looking for an exact match of the entire text.
|
| | 13 | % Any trailing space characters in STR or STRARRAY are ignored when
|
| | 14 | % matching.
|
| | 15 | %
|
| | 16 | % Examples
|
| | 17 | % idx = strmatch('max',strvcat('max','minimax','maximum'))
|
| | 18 | % returns idx = [1; 3] since rows 1 and 3 begin with 'max', and
|
| | 19 | % idx = strmatch('max',strvcat('max','minimax','maximum'),'exact')
|
| | 20 | % returns idx = 1, since only row 1 matches 'max' exactly.
|
| | 21 | %
|
| | 22 | % See also startsWith, VALIDATESTRING, CONTAINS, STRFIND, STRCMP,
|
| | 23 | % STRNCMP, REGEXP
|
| | 24 |
|
| | 25 | % Copyright 1984-2017 The MathWorks, Inc.
|
| | 26 |
|
< 0.001 | 12 | 27 | narginchk(2,3);
|
< 0.001 | 12 | 28 | missingStrs = [];
|
| | 29 |
|
< 0.001 | 12 | 30 | if nargin > 2
|
| | 31 | flag = convertStringsToChars(flag);
|
< 0.001 | 12 | 32 | end
|
| | 33 |
|
< 0.001 | 12 | 34 | if isstring(strs)
|
| | 35 | missingStrs = reshape(ismissing(strs), [], 1);
|
| | 36 | strs(missingStrs) = "";
|
< 0.001 | 12 | 37 | end
|
| | 38 |
|
< 0.001 | 12 | 39 | if (isstring(str) && any(ismissing(str))) || ((isstring(strs) || iscell(strs)) && isempty(strs))
|
| | 40 | i = [];
|
| | 41 | return
|
< 0.001 | 12 | 42 | end
|
| | 43 |
|
< 0.001 | 12 | 44 | if iscellstr(str) || isstring(str)
|
| | 45 | str = str(:);
|
| | 46 | str = char(str);
|
< 0.001 | 12 | 47 | end
|
| | 48 |
|
< 0.001 | 12 | 49 | if iscellstr(strs) || isstring(strs)
|
< 0.001 | 12 | 50 | strs = strs(:);
|
< 0.001 | 12 | 51 | strs = char(strs);
|
< 0.001 | 12 | 52 | end
|
| | 53 |
|
< 0.001 | 12 | 54 | [m,n] = size(strs);
|
< 0.001 | 12 | 55 | len = numel(str);
|
| | 56 |
|
< 0.001 | 12 | 57 | if (nargin==3)
|
| | 58 | exactMatch = true;
|
| | 59 | if ~ischar(flag)
|
| | 60 | warning(message('MATLAB:strmatch:InvalidFlagType'));
|
| | 61 | elseif ~strcmpi(flag,'exact')
|
| | 62 | warning(message('MATLAB:strmatch:InvalidFlag', flag, flag));
|
| | 63 | end
|
< 0.001 | 12 | 64 | else
|
< 0.001 | 12 | 65 | exactMatch = false;
|
< 0.001 | 12 | 66 | end
|
| | 67 |
|
| | 68 | % Special treatment for empty STR or STRS to avoid
|
| | 69 | % warnings and error below
|
< 0.001 | 12 | 70 | if len==0
|
| | 71 | str = reshape(str,1,len);
|
< 0.001 | 12 | 72 | end
|
< 0.001 | 12 | 73 | if n==0
|
| | 74 | strs = reshape(strs,max(m,1),n);
|
| | 75 | [m,n] = size(strs);
|
< 0.001 | 12 | 76 | end
|
| | 77 |
|
< 0.001 | 12 | 78 | if len > n
|
| | 79 | i = [];
|
< 0.001 | 12 | 80 | else
|
< 0.001 | 12 | 81 | if exactMatch && len < n % if 'exact' flag, pad str with blanks or nulls
|
| | 82 | [~,strn] = size(str);
|
| | 83 | if strn ~= len
|
| | 84 | error(message('MATLAB:strmatch:InvalidShape'));
|
| | 85 | else
|
| | 86 | % Use nulls if anything in the last column is a null.
|
| | 87 | null = char(0);
|
| | 88 | space = ' ';
|
| | 89 | if ~isempty(strs) && any(strs(:,end)==null)
|
| | 90 | str = [str null(ones(1,n-len))];
|
| | 91 | else
|
| | 92 | str = [str space(ones(1,n-len))];
|
| | 93 | end
|
| | 94 | len = n;
|
| | 95 | end
|
< 0.001 | 12 | 96 | end
|
| | 97 |
|
< 0.001 | 12 | 98 | mask = true(m,1);
|
| | 99 | % walk from end of strs array and search for row starting with str.
|
< 0.001 | 12 | 100 | for outer = 1:m
|
< 0.001 | 492 | 101 | for inner = 1:len
|
< 0.001 | 816 | 102 | if (strs(outer,inner) ~= str(inner))
|
< 0.001 | 432 | 103 | mask(outer) = false;
|
< 0.001 | 432 | 104 | break; % exit matching this row in strs with str.
|
< 0.001 | 384 | 105 | end
|
< 0.001 | 384 | 106 | end
|
< 0.001 | 492 | 107 | end
|
< 0.001 | 12 | 108 | mask(missingStrs) = 0;
|
< 0.001 | 12 | 109 | i = find(mask);
|
< 0.001 | 12 | 110 | end
|
Other subfunctions in this file are not included in this listing.