4779
|
1 ## Copyright (C) 2000 2004 Auburn University. All rights reserved. |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by the |
|
7 ## Free Software Foundation; either version 2, or (at your option) any |
|
8 ## later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 ## for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. |
|
18 |
|
19 ## -*- texinfo -*- |
4844
|
20 ## @deftypefn {Function File} {[@var{idxvec}, @var{errmsg}] =} cellidx (@var{listvar}, @var{strlist}) |
4779
|
21 ## Return indices of string entries in @var{listvar} that match strings |
|
22 ## in @var{strlist}. |
|
23 ## |
|
24 ## Both @var{listvar} and @var{strlist} may be passed as strings or |
|
25 ## string matrices. If they are passed as string matrices, each entry |
|
26 ## is processed by @code{deblank} prior to searching for the entries. |
|
27 ## |
|
28 ## The first output is the vector of indices in @var{listvar}. |
|
29 ## |
|
30 ## If @var{strlist} contains a string not in @var{listvar}, then |
|
31 ## an error message is returned in @var{errmsg}. If only one output |
4844
|
32 ## argument is requested, then @var{cellidx} prints @var{errmsg} to the |
4779
|
33 ## screen and exits with an error. |
|
34 ## @end deftypefn |
|
35 |
|
36 function [idxvec,errmsg] = cellidx(listvar,strlist) |
|
37 |
|
38 if(nargin != 2) |
|
39 usage("idxvec = cellidx(listvar,strlist)"); |
|
40 endif |
|
41 |
|
42 if(isstr(strlist)) |
|
43 tmp = strlist; |
|
44 strlist = {}; |
|
45 for kk=1:rows(tmp) |
|
46 strlist{kk} = deblank(tmp(kk,:)); |
|
47 endfor |
|
48 endif |
|
49 |
|
50 if(isstr(listvar)) |
|
51 tmp = listvar; |
|
52 listvar = {}; |
|
53 for kk=1:rows(tmp) |
|
54 listvar{kk} = deblank(tmp(kk,:)); |
|
55 endfor |
|
56 endif |
|
57 |
|
58 ## initialize size of idxvec (for premature return) |
|
59 idxvec = zeros(length(strlist),1); |
|
60 |
|
61 errmsg = ""; |
|
62 if(!is_signal_list(listvar)) |
|
63 errmsg = "listvar must be a list of strings"; |
|
64 elseif(!is_signal_list(strlist)) |
|
65 errmsg = "strlist must be a list of strings"; |
|
66 endif |
|
67 |
|
68 if(length(errmsg)) |
|
69 if(nargout < 2) error(errmsg); |
|
70 else return; |
|
71 endif |
|
72 endif |
|
73 |
|
74 nsigs = length(listvar); |
|
75 for idx = 1:length(strlist) |
|
76 signame = strlist{idx}; |
|
77 for jdx = 1:nsigs |
|
78 if( strcmp(signame,listvar{jdx}) ) |
|
79 if(idxvec(idx) != 0) |
|
80 warning("Duplicate signal name %s (%d,%d)\n", ... |
|
81 listvar{jdx},jdx,idxvec(idx)); |
|
82 else |
|
83 idxvec(idx) = jdx; |
|
84 endif |
|
85 endif |
|
86 endfor |
|
87 if(idxvec(idx) == 0) |
|
88 errmsg = sprintf("Did not find %s",signame); |
|
89 if(nargout == 1) |
|
90 error(errmsg); |
|
91 else |
|
92 break |
|
93 end |
|
94 endif |
|
95 endfor |
|
96 |
|
97 endfunction |