annotate scripts/strings/strmatch.m @ 6046:34f96dd5441b

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