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