5178
|
1 ## Copyright (C) 2003 Alois Schloegl |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 ## |
|
17 ## Copyright (C) 2000 Paul Kienzle |
|
18 |
|
19 ## usage: strmatch(s, A [, 'exact']) |
|
20 ## Determines which entries of A match string s. A can be a string matrix |
|
21 ## or a cell array of strings. If 'exact' is not given, then s only needs |
|
22 ## to match A up to the length of s. Null characters match blanks. |
|
23 ## Results are returned as a column vector. |
|
24 function idx = strmatch(s,A,exact) |
|
25 if (nargin < 2 || nargin > 3) |
|
26 usage("strmatch(s,A,'exact')"); |
|
27 endif |
|
28 |
|
29 try istno = implicit_str_to_num_ok; |
|
30 catch istno = 0; |
|
31 end |
|
32 try wstno = warn_str_to_num; |
|
33 catch wstno = 0; |
|
34 end |
|
35 try dfi = do_fortran_indexing; |
|
36 catch dfi = 0; |
|
37 end |
|
38 try wfi = warn_fortran_indexing; |
|
39 catch wfi = 0; |
|
40 end |
|
41 unwind_protect |
|
42 implicit_str_to_num_ok = 1; |
|
43 warn_str_to_num = 0; |
|
44 do_fortran_indexing = 1; |
|
45 warn_fortran_indexing = 0; |
|
46 |
|
47 [nr, nc] = size (A); |
|
48 if iscell(A) |
|
49 match = zeros(prod(size(A)),1); |
|
50 if nargin>2, |
|
51 for k = 1:prod(size(A)), |
|
52 match(k) = strcmp(s,A{k}); |
|
53 end |
|
54 else |
|
55 for k = 1:prod(size(A)), |
|
56 match(k) = strncmp(s,A{k},length(s)); |
|
57 end |
|
58 end |
|
59 idx = find(match); |
|
60 elseif (length (s) > nc) |
|
61 idx = []; |
|
62 else |
|
63 if (nargin == 3 && length(s) < nc) s(1,nc) = ' '; endif |
|
64 s (s==0) = ' '; |
|
65 A (A==0) = ' '; |
|
66 match = s(ones(size(A,1),1),:) == A(:,1:length(s)); |
|
67 if (length(s) == 1) |
|
68 idx = find(match); |
|
69 else |
|
70 idx = find(all(match')'); |
|
71 endif |
|
72 endif |
|
73 |
|
74 unwind_protect_cleanup |
|
75 implicit_str_to_num_ok = istno; |
|
76 warn_str_to_num = wstno; |
|
77 do_fortran_indexing = dfi; |
|
78 warn_fortran_indexing = wfi; |
|
79 end_unwind_protect |
|
80 endfunction |