Mercurial > hg > octave-nkf
annotate scripts/strings/strmatch.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 502e58a0d44f |
children | 38600f8cba83 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2005, 2006, 2007, 2009 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 | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
26 ## characters match blanks. Results are returned as a column vector. |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
27 ## For example: |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
28 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
29 ## @example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
30 ## @group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
31 ## strmatch ("apple", "apple juice") |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
32 ## @result{} 1 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
33 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
34 ## strmatch ("apple", ["apple pie"; "apple juice"; "an apple"]) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
35 ## @result{} [1; 2] |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
36 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
37 ## strmatch ("apple", @{"apple pie"; "apple juice"; "tomato"@}) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
38 ## @result{} [1; 2] |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
39 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
40 ## @end example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
41 ## @seealso{strfind, findstr, strcmp, strncmp, strcmpi, strncmpi, find} |
5182 | 42 ## @end deftypefn |
5181 | 43 |
44 ## Author: Paul Kienzle, Alois Schloegl | |
45 ## Adapted-by: jwe | |
46 | |
47 function idx = strmatch (s, A, exact) | |
48 | |
5178 | 49 if (nargin < 2 || nargin > 3) |
6046 | 50 print_usage (); |
5178 | 51 endif |
52 | |
5181 | 53 [nr, nc] = size (A); |
54 nel = numel (A); | |
55 if (iscell (A)) | |
56 match = zeros (nel, 1); | |
57 if (nargin > 2) | |
58 for k = 1:nel | |
59 match(k) = strcmp (s, A{k}); | |
5182 | 60 endfor |
5181 | 61 else |
62 for k = 1:nel | |
63 match(k) = strncmp (s, A{k}, length (s)); | |
5182 | 64 endfor |
65 endif | |
5181 | 66 idx = find (match); |
67 elseif (length (s) > nc) | |
68 idx = []; | |
69 else | |
70 if (nargin == 3 && length(s) < nc) | |
71 s(1,nc) = " "; | |
72 endif | |
73 s(s == 0) = " "; | |
74 A(A == 0) = " "; | |
75 match = s(ones(size(A,1),1),:) == A(:,1:length(s)); | |
5206 | 76 if (length (s) == 1) |
5181 | 77 idx = find (match); |
5178 | 78 else |
5181 | 79 idx = find (all (match')'); |
5178 | 80 endif |
5181 | 81 endif |
5178 | 82 |
83 endfunction | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
84 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
85 %!error <Invalid call to strmatch> strmatch(); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
86 %!error <Invalid call to strmatch> strmatch("a", "aaa", "exact", 1); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
87 %!assert (strmatch("a", {"aaa", "bab", "bbb"}), 1); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
88 %!assert (strmatch ("apple", "apple juice"), 1); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
89 %!assert (strmatch ("apple", ["apple pie"; "apple juice"; "an apple"]), |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
90 %! [1; 2]); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
91 %!assert (strmatch ("apple", {"apple pie"; "apple juice"; "tomato"}), |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
92 %! [1; 2]); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
93 %!assert (strmatch ("apple pie", "apple"), []); |