3430
|
1 ## Copyright (C) 1996, 1998 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 |
7016
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3430
|
9 ## |
7016
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
3430
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3430
|
18 |
|
19 ## -*- texinfo -*- |
3500
|
20 ## @deftypefn {Function File} {} syssetsignals (@var{sys}, @var{opt}, @var{names}, @var{sig_idx}) |
3430
|
21 ## change the names of selected inputs, outputs and states. |
5016
|
22 ## |
3430
|
23 ## @strong{Inputs} |
|
24 ## @table @var |
|
25 ## @item sys |
5016
|
26 ## System data structure. |
3430
|
27 ## |
|
28 ## @item opt |
5016
|
29 ## Change default name (output). |
3430
|
30 ## |
|
31 ## @table @code |
|
32 ## @item "out" |
5016
|
33 ## Change selected output names. |
3430
|
34 ## @item "in" |
5016
|
35 ## Change selected input names. |
3430
|
36 ## @item "st" |
5016
|
37 ## Change selected state names. |
3430
|
38 ## @item "yd" |
5016
|
39 ## Change selected outputs from discrete to continuous or |
3430
|
40 ## from continuous to discrete. |
|
41 ## @end table |
|
42 ## |
|
43 ## @item names |
|
44 ## @table @code |
5016
|
45 ## @item opt = "out", "in", "st" |
3430
|
46 ## string or string array containing desired signal names or values. |
|
47 ## @item opt = "yd" |
|
48 ## To desired output continuous/discrete flag. |
|
49 ## Set name to 0 for continuous, or 1 for discrete. |
|
50 ## @end table |
3462
|
51 ## @item sig_idx |
|
52 ## indices or names of outputs, yd, inputs, or |
|
53 ## states whose respective names/values should be changed. |
3430
|
54 ## |
4771
|
55 ## Default: replace entire cell array of names/entire yd vector. |
3430
|
56 ## @end table |
5016
|
57 ## |
3430
|
58 ## @strong{Outputs} |
5016
|
59 ## @table @var |
|
60 ## @item retsys |
|
61 ## @var{sys} with appropriate signal names changed |
|
62 ## (or @var{yd} values, where appropriate). |
|
63 ## @end table |
3430
|
64 ## |
|
65 ## @strong{Example} |
|
66 ## @example |
4771
|
67 ## octave:1> sys=ss([1 2; 3 4],[5;6],[7 8]); |
3430
|
68 ## octave:2> sys = syssetsignals(sys,"st",str2mat("Posx","Velx")); |
|
69 ## octave:3> sysout(sys) |
|
70 ## Input(s) |
|
71 ## 1: u_1 |
|
72 ## Output(s): |
|
73 ## 1: y_1 |
|
74 ## state-space form: |
|
75 ## 2 continuous states, 0 discrete states |
|
76 ## State(s): |
|
77 ## 1: Posx |
|
78 ## 2: Velx |
|
79 ## A matrix: 2 x 2 |
|
80 ## 1 2 |
|
81 ## 3 4 |
|
82 ## B matrix: 2 x 1 |
|
83 ## 5 |
|
84 ## 6 |
|
85 ## C matrix: 1 x 2 |
|
86 ## 7 8 |
|
87 ## D matrix: 1 x 1 |
|
88 ## 0 |
|
89 ## @end example |
|
90 ## @end deftypefn |
|
91 |
|
92 ## Author: John Ingram <ingraje@eng.auburn.edu> |
|
93 ## Created: August 1996 |
|
94 |
|
95 function retsys = syssetsignals (sys, opt, names, sig_idx) |
|
96 |
|
97 if (nargin < 3 | nargin > 4) |
6046
|
98 print_usage (); |
4030
|
99 elseif (!isstruct(sys)) |
3430
|
100 error("sys must be a system data structure"); |
|
101 elseif (isempty(opt)) |
|
102 opt = "out"; |
5443
|
103 elseif( ! ischar(opt) ) |
3430
|
104 error("opt must be a string"); |
|
105 elseif( ! (strcmp(opt,"out") + strcmp(opt,"yd") + ... |
|
106 strcmp(opt,"in") + strcmp(opt,"st") ) ) |
|
107 error("opt must be one of [], ""out"", ""yd"", ""in"", or ""st"""); |
|
108 elseif(nargin == 4) |
5443
|
109 if(is_signal_list(sig_idx) | ischar(sig_idx)) |
3462
|
110 ## convert to vector of indices |
|
111 if(opt == "yd") |
|
112 sig_idx = sysidx(sys,"out",sig_idx); |
|
113 else |
|
114 sig_idx = sysidx(sys,opt,sig_idx); |
|
115 endif |
|
116 endif |
|
117 |
|
118 ## check index vector |
3430
|
119 if(min(size(sig_idx)) > 1) |
|
120 disp("syssetsignals: sig_idx=") |
|
121 disp(sig_idx); |
|
122 error("sig_idx must be a vector") |
|
123 endif |
|
124 endif |
|
125 |
|
126 sig_vals = sysgetsignals(sys,opt); |
|
127 |
|
128 ## make sure it's in state space form if state names are given |
4771
|
129 if(strcmp(opt,"st")) |
|
130 sys = sysupdate(sys,"ss"); |
|
131 endif |
3430
|
132 |
|
133 if(strcmp(opt,"yd") == 0) |
|
134 ## it's a signal name list we're changing |
4771
|
135 if(!iscell(names)) |
|
136 names = {names}; |
3430
|
137 endif |
4771
|
138 if( (!is_signal_list(names)) & (!isempty(names)) ) |
5443
|
139 if(ischar(names{1})) |
4771
|
140 warning("syssetsignals(opt=%s): converting string matrix \"names\" to a cell array of strings",opt); |
|
141 tmpstr = names{1}; |
3430
|
142 for ii=1:rows(tmpstr) |
4771
|
143 names{ii} = deblank(tmpstr(ii,:)); |
3430
|
144 endfor |
|
145 else |
|
146 names |
4771
|
147 error("parameter \"names\" must be a cell array of strings"); |
3430
|
148 endif |
|
149 endif |
|
150 nsigs = length(sig_vals); |
|
151 |
|
152 if(nargin == 3) |
|
153 ## replace all signal names |
|
154 if(length(names) != nsigs) |
|
155 error("opt=%s, sig_idx omitted: names(len=%d) should have %d entries ", ... |
|
156 opt,length(names),nsigs); |
|
157 endif |
|
158 sig_idx = 1:nsigs; |
|
159 elseif(length(names) != length(sig_idx)) |
|
160 ## replace specified signal names |
|
161 error("opt=%s, sig_idx(len=%d), names(len=%d) mismatch",opt, ... |
|
162 length(sig_idx), length(names)); |
|
163 endif |
|
164 |
|
165 for ii=1:length(sig_idx) |
|
166 jj = sig_idx(ii); |
|
167 if(jj < 1 | jj > nsigs | jj != floor(jj+0.5)) |
|
168 error("opt=%s, sig_idx(%d)=%d, %e: must be an integer between 1 and %d", ... |
|
169 opt, ii, jj, jj, nsigs); |
|
170 endif |
4771
|
171 sig_vals{jj} = names{ii}; |
3430
|
172 endfor |
|
173 |
|
174 else |
|
175 ## update yd |
|
176 ## 1st check pathological case: no outputs |
|
177 nout = sysdimensions(sys,"out"); |
|
178 if(nout == 0) |
|
179 if(nargin != 3) |
|
180 error("opt=%s, %d outputs, sysgetsignals cannot take 4 arguments", ... |
|
181 yd,nout); |
|
182 endif |
|
183 if(!isempty(names)) |
|
184 error("opt=%s, %d outputs, names is not empty"); |
|
185 endif |
|
186 sigvals = []; |
|
187 else |
|
188 nsigs = length(sig_vals); |
4030
|
189 if(!isvector(names)) |
3430
|
190 error("syssetsignals: opt=yd, names(%dx%d) must be a vector", ... |
|
191 rows(names), columns(names)); |
|
192 endif |
|
193 if(nargin == 3) |
|
194 if(length(names) != nsigs) |
|
195 error("opt=yd, sig_idx omitted: names(%d) should be length(%d)", ... |
|
196 length(names), nsigs); |
|
197 endif |
|
198 sig_idx = 1:nsigs; |
|
199 elseif(length(names) != length(sig_idx)) |
|
200 error("opt=yd: length(names)=%d, length(sig_idx)=%d",length(names), ... |
|
201 length(sig_idx) ); |
|
202 endif |
|
203 |
|
204 badidx = find(names != 0 & names != 1); |
|
205 if(! isempty(badidx) ) |
|
206 for ii=1:length(badidx) |
|
207 warning("syssetsignals: opt=yd: names(%d)=%e, must be 0 or 1", ... |
|
208 badidx(ii), names(badidx(ii)) ); |
|
209 endfor |
|
210 error ("opt=yd: invalid values in names"); |
|
211 endif |
|
212 |
|
213 for ii=1:length(sig_idx) |
|
214 jj = sig_idx(ii); |
|
215 if(jj < 1 | jj > nsigs | jj != floor(jj)) |
|
216 error("sig_idx(%d)=%d, %e: must be an integer between 1 and %d", ... |
|
217 ii,jj, jj, nsigs); |
|
218 endif |
|
219 sig_vals(jj) = names(ii); |
|
220 endfor |
|
221 if(any(sig_vals == 1) & sysgettsam(sys) == 0) |
|
222 warning("Setting system sampling time to 1"); |
|
223 printf("syssetsignals: original system sampling time=0 but output(s)\n"); |
|
224 disp(find(sig_vals==1)) |
|
225 printf("are digital\n"); |
|
226 sys = syschtsam(sys,1); |
|
227 endif |
|
228 |
|
229 endif |
|
230 endif |
|
231 |
|
232 if(strcmp(opt,"st")) |
|
233 sys.stname = sig_vals; |
|
234 elseif(strcmp(opt,"in")) |
|
235 sys.inname = sig_vals; |
|
236 elseif(strcmp(opt,"out")) |
|
237 sys.outname = sig_vals; |
|
238 elseif(strcmp(opt,"yd")) |
|
239 sys.yd = sig_vals; |
|
240 endif |
|
241 |
|
242 retsys = sys; |
|
243 |
|
244 endfunction |