7017
|
1 ## Copyright (C) 2000, 2004, 2005, 2006, 2007 |
|
2 ## Auburn University. All rights reserved. |
3431
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
7016
|
7 ## under the terms of the GNU General Public License as published by |
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3431
|
10 ## |
7016
|
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. |
3431
|
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/>. |
3431
|
19 |
3452
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{idxvec}, @var{errmsg}] =} listidx (@var{listvar}, @var{strlist}) |
|
22 ## Return indices of string entries in @var{listvar} that match strings |
|
23 ## in @var{strlist}. |
3431
|
24 ## |
3452
|
25 ## Both @var{listvar} and @var{strlist} may be passed as strings or |
|
26 ## string matrices. If they are passed as string matrices, each entry |
|
27 ## is processed by @code{deblank} prior to searching for the entries. |
|
28 ## |
|
29 ## The first output is the vector of indices in @var{listvar}. |
|
30 ## |
|
31 ## If @var{strlist} contains a string not in @var{listvar}, then |
|
32 ## an error message is returned in @var{errmsg}. If only one output |
|
33 ## argument is requested, then @var{listidx} prints @var{errmsg} to the |
|
34 ## screen and exits with an error. |
|
35 ## @end deftypefn |
3431
|
36 |
|
37 function [idxvec,errmsg] = listidx(listvar,strlist) |
4771
|
38 error("listidx: don't use this anymore, ok?\n"); |
3431
|
39 |
|
40 if(nargin != 2) |
6046
|
41 print_usage (); |
3431
|
42 endif |
|
43 |
5443
|
44 if(ischar(strlist)) |
3431
|
45 tmp = strlist; |
|
46 strlist = list(); |
|
47 for kk=1:rows(tmp) |
|
48 strlist(kk) = deblank(tmp(kk,:)); |
|
49 endfor |
|
50 endif |
|
51 |
5443
|
52 if(ischar(listvar)) |
3431
|
53 tmp = listvar; |
|
54 listvar = list(); |
|
55 for kk=1:rows(tmp) |
|
56 listvar(kk) = deblank(tmp(kk,:)); |
|
57 endfor |
|
58 endif |
|
59 |
|
60 ## initialize size of idxvec (for premature return) |
|
61 idxvec = zeros(length(strlist),1); |
|
62 |
|
63 errmsg = ""; |
|
64 if(!is_signal_list(listvar)) |
|
65 errmsg = "listvar must be a list of strings"; |
|
66 elseif(!is_signal_list(strlist)) |
|
67 errmsg = "strlist must be a list of strings"; |
|
68 endif |
|
69 |
|
70 if(length(errmsg)) |
|
71 if(nargout < 2) error(errmsg); |
|
72 else return; |
|
73 endif |
|
74 endif |
|
75 |
|
76 nsigs = length(listvar); |
|
77 for idx = 1:length(strlist) |
4771
|
78 signame = strlist{idx}; |
3431
|
79 for jdx = 1:nsigs |
4771
|
80 if( strcmp(signame,listvar{jdx}) ) |
3431
|
81 if(idxvec(idx) != 0) |
|
82 warning("Duplicate signal name %s (%d,%d)\n", ... |
4771
|
83 listvar{jdx},jdx,idxvec(idx)); |
3431
|
84 else |
|
85 idxvec(idx) = jdx; |
|
86 endif |
|
87 endif |
|
88 endfor |
|
89 if(idxvec(idx) == 0) |
|
90 errmsg = sprintf("Did not find %s",signame); |
|
91 if(nargout == 1) |
|
92 error(errmsg); |
|
93 else |
|
94 break |
|
95 end |
|
96 endif |
|
97 endfor |
|
98 |
|
99 endfunction |