7017
|
1 ## Copyright (C) 2000, 2005, 2006, 2007 Paul Kienzle |
5178
|
2 ## Copyright (C) 2003 Alois Schloegl |
|
3 ## |
5181
|
4 ## This file is part of Octave. |
5178
|
5 ## |
5181
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
5181
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
5178
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
5178
|
19 |
5182
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} strmatch (@var{s}, @var{a}, "exact") |
6701
|
22 ## Return indices of entries of @var{a} that match the string @var{s}. |
5182
|
23 ## The second argument @var{a} may be a string matrix or a cell array of |
|
24 ## strings. If the third argument @code{"exact"} is not given, then |
|
25 ## @var{s} only needs to match @var{a} up to the length of @var{s}. Nul |
|
26 ## characters match blanks. Results are returned as a column vector. |
|
27 ## @end deftypefn |
5181
|
28 |
|
29 ## Author: Paul Kienzle, Alois Schloegl |
|
30 ## Adapted-by: jwe |
|
31 |
|
32 function idx = strmatch (s, A, exact) |
|
33 |
5178
|
34 if (nargin < 2 || nargin > 3) |
6046
|
35 print_usage (); |
5178
|
36 endif |
|
37 |
5181
|
38 [nr, nc] = size (A); |
|
39 nel = numel (A); |
|
40 if (iscell (A)) |
|
41 match = zeros (nel, 1); |
|
42 if (nargin > 2) |
|
43 for k = 1:nel |
|
44 match(k) = strcmp (s, A{k}); |
5182
|
45 endfor |
5181
|
46 else |
|
47 for k = 1:nel |
|
48 match(k) = strncmp (s, A{k}, length (s)); |
5182
|
49 endfor |
|
50 endif |
5181
|
51 idx = find (match); |
|
52 elseif (length (s) > nc) |
|
53 idx = []; |
|
54 else |
|
55 if (nargin == 3 && length(s) < nc) |
|
56 s(1,nc) = " "; |
|
57 endif |
|
58 s(s == 0) = " "; |
|
59 A(A == 0) = " "; |
|
60 match = s(ones(size(A,1),1),:) == A(:,1:length(s)); |
5206
|
61 if (length (s) == 1) |
5181
|
62 idx = find (match); |
5178
|
63 else |
5181
|
64 idx = find (all (match')'); |
5178
|
65 endif |
5181
|
66 endif |
5178
|
67 |
|
68 endfunction |