5181
|
1 ## Copyright (C) 2000 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 |
|
8 ## the Free Software Foundation; either version 2, or (at your option) |
|
9 ## any later version. |
|
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 |
5181
|
17 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
18 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
19 ## 02110-1301, USA. |
5178
|
20 |
5182
|
21 ## -*- texinfo -*- |
|
22 ## @deftypefn {Function File} {} strmatch (@var{s}, @var{a}, "exact") |
6701
|
23 ## Return indices of entries of @var{a} that match the string @var{s}. |
5182
|
24 ## The second argument @var{a} may be a string matrix or a cell array of |
|
25 ## strings. If the third argument @code{"exact"} is not given, then |
|
26 ## @var{s} only needs to match @var{a} up to the length of @var{s}. Nul |
|
27 ## characters match blanks. Results are returned as a column vector. |
|
28 ## @end deftypefn |
5181
|
29 |
|
30 ## Author: Paul Kienzle, Alois Schloegl |
|
31 ## Adapted-by: jwe |
|
32 |
|
33 function idx = strmatch (s, A, exact) |
|
34 |
5178
|
35 if (nargin < 2 || nargin > 3) |
6046
|
36 print_usage (); |
5178
|
37 endif |
|
38 |
5181
|
39 [nr, nc] = size (A); |
|
40 nel = numel (A); |
|
41 if (iscell (A)) |
|
42 match = zeros (nel, 1); |
|
43 if (nargin > 2) |
|
44 for k = 1:nel |
|
45 match(k) = strcmp (s, A{k}); |
5182
|
46 endfor |
5181
|
47 else |
|
48 for k = 1:nel |
|
49 match(k) = strncmp (s, A{k}, length (s)); |
5182
|
50 endfor |
|
51 endif |
5181
|
52 idx = find (match); |
|
53 elseif (length (s) > nc) |
|
54 idx = []; |
|
55 else |
|
56 if (nargin == 3 && length(s) < nc) |
|
57 s(1,nc) = " "; |
|
58 endif |
|
59 s(s == 0) = " "; |
|
60 A(A == 0) = " "; |
|
61 match = s(ones(size(A,1),1),:) == A(:,1:length(s)); |
5206
|
62 if (length (s) == 1) |
5181
|
63 idx = find (match); |
5178
|
64 else |
5181
|
65 idx = find (all (match')'); |
5178
|
66 endif |
5181
|
67 endif |
5178
|
68 |
|
69 endfunction |