]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/@lti/set.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / @lti / set.m
1 ## Copyright (C) 2009, 2010, 2012   Lukas F. Reichlin
2 ##
3 ## This file is part of LTI Syncope.
4 ##
5 ## LTI Syncope is free software: you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation, either version 3 of the License, or
8 ## (at your option) any later version.
9 ##
10 ## LTI Syncope is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ## GNU General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} set (@var{sys})
20 ## @deftypefnx {Function File} set (@var{sys}, @var{"property"}, @var{value}, @dots{})
21 ## @deftypefnx {Function File} {@var{retsys} =} set (@var{sys}, @var{"property"}, @var{value}, @dots{})
22 ## Set or modify properties of LTI objects.
23 ## If no return argument @var{retsys} is specified, the modified LTI object is stored
24 ## in input argument @var{sys}.  @command{set} can handle multiple properties in one call:
25 ## @code{set (sys, 'prop1', val1, 'prop2', val2, 'prop3', val3)}.
26 ## @code{set (sys)} prints a list of the object's property names.
27 ## @end deftypefn
28
29 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
30 ## Created: October 2009
31 ## Version: 0.4
32
33 function retsys = set (sys, varargin)
34
35   if (nargin == 1)       # set (sys), sys = set (sys)
36
37     [props, vals] = __property_names__ (sys);
38     nrows = numel (props);
39
40     str = strjust (strvcat (props), "right");
41     str = horzcat (repmat ("   ", nrows, 1), str, repmat (":  ", nrows, 1), strvcat (vals));
42
43     disp (str);
44
45     if (nargout != 0)    # function sys = set (sys, varargin)
46       retsys = sys;      # would lead to unwanted output when using
47     endif                # set (sys)
48
49   else                   # set (sys, "prop1", val1, ...), sys = set (sys, "prop1", val1, ...)
50
51     if (rem (nargin-1, 2))
52       error ("lti: set: properties and values must come in pairs");
53     endif
54
55     [p, m] = size (sys);
56
57     for k = 1 : 2 : (nargin-1)
58       prop = lower (varargin{k});
59       val = varargin{k+1};
60
61       switch (prop)
62         case {"inname", "inputname"}
63           sys.inname = __adjust_labels__ (val, m);
64
65         case {"outname", "outputname"}
66           sys.outname = __adjust_labels__ (val, p);
67
68         case {"tsam", "ts"}
69           if (issample (val, -1))
70             sys.tsam = val;
71             warning ("lti: set: use the editing of property '%s' with caution", prop);
72             warning ("          it may lead to corrupted models");
73           else
74             error ("lti: set: invalid sampling time");
75           endif
76           ## TODO: use of c2d, d2c and d2d if tsam changes?
77
78         case "name"
79           if (ischar (val))
80             sys.name = val;
81           else
82             error ("lti: set: property 'name' requires a string");
83           endif
84
85         case "notes"
86           if (iscellstr (val))
87             sys.notes = val;
88           elseif (ischar (val))
89             sys.notes = {val};
90           else
91             error ("lti: set: property 'notes' requires string or cell of strings");
92           endif
93
94         case "userdata"
95           sys.userdata = val;
96
97         otherwise
98           sys = __set__ (sys, prop, val);
99       endswitch
100     endfor
101
102     if (nargout == 0)    # set (sys, "prop1", val1, ...)
103       assignin ("caller", inputname (1), sys);
104     else                 # sys = set (sys, "prop1", val1, ...)
105       retsys = sys;
106     endif
107
108   endif
109
110 endfunction