Mercurial > hg > octave-lyh
comparison scripts/strings/strmatch.m @ 5181:41cd70503c72
[project @ 2005-03-03 05:49:55 by jwe]
author | jwe |
---|---|
date | Thu, 03 Mar 2005 05:49:55 +0000 |
parents | 6758c11b5b99 |
children | 5b361aa47dff |
comparison
equal
deleted
inserted
replaced
5180:e7438487c857 | 5181:41cd70503c72 |
---|---|
1 ## Copyright (C) 2000 Paul Kienzle | |
1 ## Copyright (C) 2003 Alois Schloegl | 2 ## Copyright (C) 2003 Alois Schloegl |
2 ## | 3 ## |
3 ## This program is free software; you can redistribute it and/or modify | 4 ## This file is part of Octave. |
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 ## | 5 ## |
8 ## This program is distributed in the hope that it will be useful, | 6 ## Octave is free software; you can redistribute it and/or modify it |
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | 7 ## under the terms of the GNU General Public License as published by |
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 8 ## the Free Software Foundation; either version 2, or (at your option) |
11 ## GNU General Public License for more details. | 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. | |
12 ## | 15 ## |
13 ## You should have received a copy of the GNU General Public License | 16 ## You should have received a copy of the GNU General Public License |
14 ## along with this program; if not, write to the Free Software | 17 ## along with Octave; see the file COPYING. If not, write to the Free |
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 18 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
16 ## | 19 ## 02111-1307, USA. |
17 ## Copyright (C) 2000 Paul Kienzle | |
18 | 20 |
19 ## usage: strmatch(s, A [, 'exact']) | 21 ## usage: strmatch(s, A [, 'exact']) |
20 ## Determines which entries of A match string s. A can be a string matrix | 22 ## 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 | 23 ## 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. | 24 ## to match A up to the length of s. Null characters match blanks. |
23 ## Results are returned as a column vector. | 25 ## Results are returned as a column vector. |
24 function idx = strmatch(s,A,exact) | 26 |
27 ## Author: Paul Kienzle, Alois Schloegl | |
28 ## Adapted-by: jwe | |
29 | |
30 function idx = strmatch (s, A, exact) | |
31 | |
25 if (nargin < 2 || nargin > 3) | 32 if (nargin < 2 || nargin > 3) |
26 usage("strmatch(s,A,'exact')"); | 33 usage ("strmatch (s, A, \"exact\")"); |
27 endif | 34 endif |
28 | 35 |
29 try istno = implicit_str_to_num_ok; | 36 [nr, nc] = size (A); |
30 catch istno = 0; | 37 nel = numel (A); |
31 end | 38 if (iscell (A)) |
32 try wstno = warn_str_to_num; | 39 match = zeros (nel, 1); |
33 catch wstno = 0; | 40 if (nargin > 2) |
34 end | 41 for k = 1:nel |
35 try dfi = do_fortran_indexing; | 42 match(k) = strcmp (s, A{k}); |
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 | 43 end |
59 idx = find(match); | |
60 elseif (length (s) > nc) | |
61 idx = []; | |
62 else | 44 else |
63 if (nargin == 3 && length(s) < nc) s(1,nc) = ' '; endif | 45 for k = 1:nel |
64 s (s==0) = ' '; | 46 match(k) = strncmp (s, A{k}, length (s)); |
65 A (A==0) = ' '; | 47 end |
66 match = s(ones(size(A,1),1),:) == A(:,1:length(s)); | 48 end |
67 if (length(s) == 1) | 49 idx = find (match); |
68 idx = find(match); | 50 elseif (length (s) > nc) |
69 else | 51 idx = []; |
70 idx = find(all(match')'); | 52 else |
71 endif | 53 if (nargin == 3 && length(s) < nc) |
54 s(1,nc) = " "; | |
72 endif | 55 endif |
56 s(s == 0) = " "; | |
57 A(A == 0) = " "; | |
58 match = s(ones(size(A,1),1),:) == A(:,1:length(s)); | |
59 if (length(s) == 1) | |
60 idx = find (match); | |
61 else | |
62 idx = find (all (match')'); | |
63 endif | |
64 endif | |
73 | 65 |
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 | 66 endfunction |