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