changeset 9456:5b575a06d7b8 octave-forge

control-devel: add get/set methods to iddata class
author paramaniac
date Tue, 21 Feb 2012 19:27:53 +0000
parents 48dea1a838d2
children 1614cd3d4080
files extra/control-devel/devel/test_iddata.m extra/control-devel/inst/@iddata/__property_names__.m extra/control-devel/inst/@iddata/get.m extra/control-devel/inst/@iddata/iddata.m extra/control-devel/inst/@iddata/plot.m extra/control-devel/inst/@iddata/set.m extra/control-devel/inst/__adjust_iddata__.m
diffstat 7 files changed, 321 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/extra/control-devel/devel/test_iddata.m	Tue Feb 21 14:23:31 2012 +0000
+++ b/extra/control-devel/devel/test_iddata.m	Tue Feb 21 19:27:53 2012 +0000
@@ -2,4 +2,6 @@
 
 a = iddata ({(1:10).', (21:30).'}, {(31:40).', (41:50).'})
 
-b = iddata ({(1:10).', (21:30).'}, [])
\ No newline at end of file
+b = iddata ({(1:10).', (21:30).'}, [])
+
+c = iddata ({(1:10).', (21:40).'}, {(31:40).', (41:60).'})
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/control-devel/inst/@iddata/__property_names__.m	Tue Feb 21 19:27:53 2012 +0000
@@ -0,0 +1,57 @@
+## Copyright (C) 2012   Lukas F. Reichlin
+##
+## This file is part of LTI Syncope.
+##
+## LTI Syncope is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## LTI Syncope is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {[@var{props}, @var{vals}] =} __property_names__ (@var{sys})
+## Return the list of properties as well as the assignable values for an LTI object sys.
+## @end deftypefn
+
+## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
+## Created: February 2012
+## Version: 0.1
+
+function [props, vals] = __property_names__ (dat)
+
+  ## cell vector of iddata-specific properties
+  props = {"y";
+           "outname";
+           "outunit";
+           "u";
+           "inname";
+           "inunit";
+           "tsam";
+           "timeunit";
+           "exname";
+           "name";
+           "notes";
+           "userdata"};
+
+  ## cell vector of lti-specific assignable values
+  vals = {"p-by-1 cell vector of matrices";
+          "p-by-1 cell vector of strings";
+          "p-by-1 cell vector of strings";
+          "m-by-1 cell vector of matrices";
+          "m-by-1 cell vector of strings";
+          "m-by-1 cell vector of strings";
+          "scalar (sample time in seconds)";
+          "string";
+          "e-by-1 cell vector of strings";
+          "string";
+          "string or cell of strings";
+          "any data type"};
+
+endfunction
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/control-devel/inst/@iddata/get.m	Tue Feb 21 19:27:53 2012 +0000
@@ -0,0 +1,73 @@
+## Copyright (C) 2012   Lukas F. Reichlin
+##
+## This file is part of LTI Syncope.
+##
+## LTI Syncope is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## LTI Syncope is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} get (@var{sys})
+## @deftypefnx {Function File} {@var{value} =} get (@var{sys}, @var{"property"})
+## Access property values of LTI objects.
+## @end deftypefn
+
+## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
+## Created: February 2012
+## Version: 0.1
+
+function varargout = get (dat, varargin)
+
+  if (nargin == 1)
+    [props, vals] = __property_names__ (dat);
+    nrows = numel (props);
+    str = strjust (strvcat (props), "right");
+    str = horzcat (repmat ("   ", nrows, 1), str, repmat (":  ", nrows, 1), strvcat (vals));
+    disp (str);
+  else
+    for k = 1 : (nargin-1)
+      prop = lower (varargin{k});
+
+      switch (prop)
+        case {"y", "outdata", "outputdata"}
+          val = dat.y;
+        case {"u", "indata", "inputdata"}
+          val = dat.u;
+        case {"outname", "outputname"}
+          val = dat.outname;
+        case {"inname", "inputname"}
+          val = dat.inname;
+        case {"outunit", "outputunit"}
+          val = dat.outunit;
+        case {"inunit", "inputunit"}
+          val = dat.inunit;
+        case {"tsam", "ts"}
+          val = dat.tsam;
+        case {"timeunit"}
+          val = dat.timeunit
+        case {"exname", "experimentname"}
+          val = dat.exname;
+        case "name"
+          val = dat.name;
+        case "notes"
+          val = dat.notes;
+        case "userdata"
+          val = dat.userdata;
+        otherwise
+          error ("iddata: get: invalid property name '%s'", varargin{k});
+      endswitch
+
+      varargout{k} = val;
+    endfor
+  endif
+
+endfunction
\ No newline at end of file
--- a/extra/control-devel/inst/@iddata/iddata.m	Tue Feb 21 14:23:31 2012 +0000
+++ b/extra/control-devel/inst/@iddata/iddata.m	Tue Feb 21 19:27:53 2012 +0000
@@ -92,25 +92,6 @@
 endfunction
 
 
-function [y, u] = __adjust_iddata__ (y, u)
-
-  if (iscell (y))
-    y = reshape (y, [], 1);
-  else
-    y = {y};
-  endif
-  
-  if (isempty (u))
-    u = [];     # avoid [](nx0) and the like
-  elseif (iscell (u))
-    u = reshape (u, [], 1);
-  else
-    u = {u};
-  endif
-
-endfunction
-
-
 %!error (iddata);
 %!error (iddata ((1:10).', (1:11).'));
 %!warning (iddata (1:10));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/control-devel/inst/@iddata/plot.m	Tue Feb 21 19:27:53 2012 +0000
@@ -0,0 +1,23 @@
+function plot (dat)
+
+  [n, p, m, e] = size (dat)
+
+  if (m == 0)  # time series
+    for k = 1 : e
+      plot (dat.y{k})
+      hold on
+    endfor
+  else         # inputs present
+    for k = 1 : e
+      subplot (2, 1, 1)
+      plot (dat.y{k})
+      hold on
+      subplot (2, 1, 2)
+      stairs (dat.u{k})
+      hold on
+    endfor
+  endif
+  
+  hold off
+
+endfunction
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/control-devel/inst/@iddata/set.m	Tue Feb 21 19:27:53 2012 +0000
@@ -0,0 +1,127 @@
+## Copyright (C) 2012   Lukas F. Reichlin
+##
+## This file is part of LTI Syncope.
+##
+## LTI Syncope is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## LTI Syncope is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} set (@var{sys})
+## @deftypefnx {Function File} set (@var{sys}, @var{"property"}, @var{value}, @dots{})
+## @deftypefnx {Function File} {@var{retsys} =} set (@var{sys}, @var{"property"}, @var{value}, @dots{})
+## Set or modify properties of LTI objects.
+## If no return argument @var{retsys} is specified, the modified LTI object is stored
+## in input argument @var{sys}.  @command{set} can handle multiple properties in one call:
+## @code{set (sys, 'prop1', val1, 'prop2', val2, 'prop3', val3)}.
+## @code{set (sys)} prints a list of the object's property names.
+## @end deftypefn
+
+## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
+## Created: February 2012
+## Version: 0.1
+
+function retdat = set (dat, varargin)
+
+  if (nargin == 1)       # set (dat), dat = set (dat)
+
+    [props, vals] = __property_names__ (dat);
+    nrows = numel (props);
+
+    str = strjust (strvcat (props), "right");
+    str = horzcat (repmat ("   ", nrows, 1), str, repmat (":  ", nrows, 1), strvcat (vals));
+
+    disp (str);
+
+    if (nargout != 0)    # function dat = set (dat, varargin)
+      retdat = dat;      # would lead to unwanted output when using
+    endif                # set (dat)
+
+  else                   # set (dat, "prop1", val1, ...), sys = set (dat, "prop1", val1, ...)
+
+    if (rem (nargin-1, 2))
+      error ("iddata: set: properties and values must come in pairs");
+    endif
+
+    [n, p, m, e] = size (dat);
+
+    for k = 1 : 2 : (nargin-1)
+      prop = lower (varargin{k});
+      val = varargin{k+1};
+
+      switch (prop)
+        case {"y", "outdata", "outputdata"}
+          val = __adjust_iddata__ (val, dat.u);
+          __iddata_dim__ (val, dat.u);
+          dat.y = val;
+        case {"u", "indata", "inputdata"}
+          [~, val] = __adjust_iddata__ (dat.y, val);
+          __iddata_dim__ (dat.y, val);
+          dat.u = val;
+        case {"outname", "outputname"}
+          dat.outname = __adjust_labels__ (val, p);
+        case {"inname", "inputname"}
+          dat.inname = __adjust_labels__ (val, m);
+        case {"outunit", "outputunit"}
+          dat.outunit = __adjust_labels__ (val, p);
+        case {"inunit", "inputunit"}
+          dat.inunit = __adjust_labels__ (val, m);
+        case {"tsam", "ts"}
+          dat.tsam;
+        case {"timeunit"}
+          dat.timeunit
+        case {"exname", "experimentname"}
+          dat.exname = __adjust_labels__ (val, e);
+
+        case {"tsam", "ts"}
+          if (issample (val, -1))
+            sys.tsam = val;
+            warning ("lti: set: use the editing of property '%s' with caution", prop);
+            warning ("          it may lead to corrupted models");
+          else
+            error ("lti: set: invalid sampling time");
+          endif
+          ## TODO: use of c2d, d2c and d2d if tsam changes?
+
+        case "name"
+          if (ischar (val))
+            sys.name = val;
+          else
+            error ("lti: set: property 'name' requires a string");
+          endif
+
+        case "notes"
+          if (iscellstr (val))
+            sys.notes = val;
+          elseif (ischar (val))
+            sys.notes = {val};
+          else
+            error ("lti: set: property 'notes' requires string or cell of strings");
+          endif
+
+        case "userdata"
+          sys.userdata = val;
+
+        otherwise
+          error ("iddata: set: invalid property name '%s'", varargin{k});
+      endswitch
+    endfor
+
+    if (nargout == 0)    # set (sys, "prop1", val1, ...)
+      assignin ("caller", inputname (1), dat);
+    else                 # dat = set (dat, "prop1", val1, ...)
+      retdat = dat;
+    endif
+
+  endif
+
+endfunction
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/control-devel/inst/__adjust_iddata__.m	Tue Feb 21 19:27:53 2012 +0000
@@ -0,0 +1,38 @@
+## Copyright (C) 2012   Lukas F. Reichlin
+##
+## This file is part of LTI Syncope.
+##
+## LTI Syncope is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## LTI Syncope is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.
+
+## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
+## Created: February 2012
+## Version: 0.1
+
+function [y, u] = __adjust_iddata__ (y, u)
+
+  if (iscell (y))
+    y = reshape (y, [], 1);
+  else
+    y = {y};
+  endif
+  
+  if (isempty (u))
+    u = [];     # avoid [](nx0) and the like
+  elseif (iscell (u))
+    u = reshape (u, [], 1);
+  else
+    u = {u};
+  endif
+
+endfunction