7017
|
1 ## Copyright (C) 1996, 1998, 2000, 2002, 2004, 2005, 2006, 2007 |
|
2 ## Auburn University. All rights reserved. |
3430
|
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. |
3430
|
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. |
3430
|
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/>. |
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 |
7031
|
68 ## octave:1> sys=ss ([1 2; 3 4],[5;6],[7 8]); |
|
69 ## octave:2> sys = syssetsignals (sys, "st", |
|
70 ## > str2mat("Posx","Velx")); |
3430
|
71 ## octave:3> sysout(sys) |
|
72 ## Input(s) |
|
73 ## 1: u_1 |
|
74 ## Output(s): |
|
75 ## 1: y_1 |
|
76 ## state-space form: |
|
77 ## 2 continuous states, 0 discrete states |
|
78 ## State(s): |
|
79 ## 1: Posx |
|
80 ## 2: Velx |
|
81 ## A matrix: 2 x 2 |
|
82 ## 1 2 |
|
83 ## 3 4 |
|
84 ## B matrix: 2 x 1 |
|
85 ## 5 |
|
86 ## 6 |
|
87 ## C matrix: 1 x 2 |
|
88 ## 7 8 |
|
89 ## D matrix: 1 x 1 |
|
90 ## 0 |
|
91 ## @end example |
|
92 ## @end deftypefn |
|
93 |
|
94 ## Author: John Ingram <ingraje@eng.auburn.edu> |
|
95 ## Created: August 1996 |
|
96 |
|
97 function retsys = syssetsignals (sys, opt, names, sig_idx) |
|
98 |
7135
|
99 if (nargin < 3 || nargin > 4) |
6046
|
100 print_usage (); |
7135
|
101 elseif (! isstruct (sys)) |
|
102 error ("sys must be a system data structure"); |
|
103 elseif (isempty (opt)) |
3430
|
104 opt = "out"; |
7135
|
105 elseif (! ischar (opt)) |
|
106 error ("opt must be a string"); |
|
107 elseif (! (strcmp (opt, "out") || strcmp (opt, "yd") |
|
108 || strcmp (opt, "in") || strcmp (opt, "st"))) |
|
109 error ("opt must be one of [], \"out\", \"yd\", \"in\", or \"st\""); |
|
110 elseif (nargin == 4) |
|
111 if (is_signal_list (sig_idx) || ischar (sig_idx)) |
3462
|
112 ## convert to vector of indices |
7135
|
113 if (opt == "yd") |
|
114 sig_idx = sysidx (sys, "out", sig_idx); |
3462
|
115 else |
7135
|
116 sig_idx = sysidx (sys, opt, sig_idx); |
3462
|
117 endif |
|
118 endif |
|
119 |
|
120 ## check index vector |
7135
|
121 if (min (size (sig_idx)) > 1) |
|
122 disp ("syssetsignals: sig_idx=") |
|
123 disp (sig_idx); |
|
124 error ("sig_idx must be a vector") |
3430
|
125 endif |
|
126 endif |
|
127 |
7135
|
128 sig_vals = sysgetsignals (sys, opt); |
3430
|
129 |
|
130 ## make sure it's in state space form if state names are given |
7135
|
131 if (strcmp (opt, "st")) |
|
132 sys = sysupdate (sys, "ss"); |
4771
|
133 endif |
3430
|
134 |
7135
|
135 if (strcmp (opt, "yd") == 0) |
3430
|
136 ## it's a signal name list we're changing |
7135
|
137 if (! iscell (names)) |
4771
|
138 names = {names}; |
3430
|
139 endif |
7135
|
140 if (! is_signal_list (names) && ! isempty (names)) |
|
141 if (ischar (names{1})) |
4771
|
142 tmpstr = names{1}; |
3430
|
143 for ii=1:rows(tmpstr) |
4771
|
144 names{ii} = deblank(tmpstr(ii,:)); |
3430
|
145 endfor |
|
146 else |
7135
|
147 error ("parameter \"names\" must be a cell array of strings"); |
3430
|
148 endif |
|
149 endif |
7135
|
150 nsigs = length (sig_vals); |
3430
|
151 |
7135
|
152 if (nargin == 3) |
3430
|
153 ## replace all signal names |
7135
|
154 if (length (names) != nsigs) |
|
155 error ("opt=%s, sig_idx omitted: names(len=%d) should have %d entries ", |
|
156 opt, length (names), nsigs); |
3430
|
157 endif |
|
158 sig_idx = 1:nsigs; |
7135
|
159 elseif (length (names) != length (sig_idx)) |
3430
|
160 ## replace specified signal names |
7135
|
161 error ("opt=%s, sig_idx(len=%d), names(len=%d) mismatch", |
|
162 opt, length (sig_idx), length (names)); |
3430
|
163 endif |
|
164 |
7135
|
165 for ii = 1:length(sig_idx) |
3430
|
166 jj = sig_idx(ii); |
7135
|
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); |
3430
|
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 |
7135
|
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 opt, nout); |
3430
|
182 endif |
7135
|
183 if (! isempty (names)) |
|
184 error ("opt=%s, %d outputs, names is not empty", opt, nout); |
3430
|
185 endif |
|
186 sigvals = []; |
|
187 else |
7135
|
188 nsigs = length (sig_vals); |
|
189 if (! isvector (names)) |
|
190 error ("syssetsignals: opt=%s, names(%dx%d) must be a vector", |
|
191 opt, rows (names), columns (names)); |
3430
|
192 endif |
7135
|
193 if (nargin == 3) |
|
194 if (length (names) != nsigs) |
|
195 error ("opt=%s, sig_idx omitted: names(%d) should be length(%d)", |
|
196 opt, length (names), nsigs); |
3430
|
197 endif |
|
198 sig_idx = 1:nsigs; |
7135
|
199 elseif (length(names) != length (sig_idx)) |
|
200 error ("opt=%s: length(names)=%d, length(sig_idx)=%d", |
|
201 opt, length (names), length (sig_idx)); |
3430
|
202 endif |
|
203 |
7135
|
204 badidx = find (names != 0 & names != 1); |
|
205 if (! isempty (badidx)) |
|
206 for ii = 1:length(badidx) |
|
207 warning ("syssetsignals: opt=%s: names(%d)=%e, must be 0 or 1", |
|
208 opt, badidx(ii), names(badidx(ii)) ); |
3430
|
209 endfor |
7135
|
210 error ("opt=%s: invalid values in names", opt); |
3430
|
211 endif |
|
212 |
7135
|
213 for ii = 1:length(sig_idx) |
3430
|
214 jj = sig_idx(ii); |
7135
|
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); |
3430
|
218 endif |
|
219 sig_vals(jj) = names(ii); |
|
220 endfor |
7135
|
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); |
3430
|
227 endif |
|
228 |
|
229 endif |
|
230 endif |
|
231 |
7135
|
232 if (strcmp (opt, "st")) |
3430
|
233 sys.stname = sig_vals; |
7135
|
234 elseif (strcmp (opt, "in")) |
3430
|
235 sys.inname = sig_vals; |
7135
|
236 elseif (strcmp (opt, "out")) |
3430
|
237 sys.outname = sig_vals; |
7135
|
238 elseif (strcmp (opt, "yd")) |
3430
|
239 sys.yd = sig_vals; |
|
240 endif |
|
241 |
|
242 retsys = sys; |
|
243 |
|
244 endfunction |