home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / control / syssetsignals.m < prev    next >
Text File  |  1999-03-05  |  6KB  |  169 lines

  1. # Copyright (C) 1996,1998 A. Scottedward Hodel 
  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. # Octave is distributed in the hope that it will be useful, but WITHOUT 
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  11. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
  12. # for more details.
  13. # You should have received a copy of the GNU General Public License 
  14. # along with Octave; see the file COPYING.  If not, write to the Free 
  15. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  16.  
  17. function retsys = syssetsignals(sys,opt,names,sig_idx)
  18. # retsys = syssetsignals(sys,opt,names{,sig_idx})
  19. # change the names of selected inputs, outputs and states.
  20. # inputs:
  21. #     sys: system data structure
  22. #    opt: []: change default name (output)
  23. #         "out": change selected output names
  24. #         "in": change selected input names
  25. #         "st": change selected state names     
  26. #         "yd": change selected outputs from discrete to continuous or 
  27. #           from continuous to discrete.
  28. #        names: opt = "out", "in", or "st": string or or list of strings
  29. #              opt = "yd": desired output continuous/discrete flag.
  30. #                          names(ii) = 0: output ii is continuous
  31. #                          names(ii) = 1: output ii is discrete
  32. #         list: vector of indices of outputs, yd, inputs, or
  33. #             states whose respective names should be changed.
  34. #             Default: replace entire signal list/vector value
  35. # outputs:
  36. #    retsys=sys with appropriate signal names changed 
  37. #            (or yd values, where appropriate)
  38.  
  39. # Written by John Ingram August 1996
  40.  
  41.   save_val = implicit_str_to_num_ok;    # save for later
  42.   implicit_str_to_num_ok = 1;
  43.  
  44.   if (nargin < 3 | nargin > 4)
  45.     usage("retsys=syssetsignals(sys,opt,names{,sig_idx})");
  46.   elseif (!is_struct(sys))
  47.     error("sys must be a system data structure");
  48.   elseif (isempty(opt))
  49.     opt = "out";
  50.   elseif( ! isstr(opt)  )
  51.     error("opt must be a string");
  52.   elseif( ! (strcmp(opt,"out") + strcmp(opt,"yd") + ...
  53.     strcmp(opt,"in") + strcmp(opt,"st") ) )
  54.     error("opt must be one of [], ""out"", ""yd"", ""in"", or ""st""");
  55.   elseif(nargin == 4)
  56.     if(min(size(sig_idx)) > 1)
  57.       disp("syssetsignals: sig_idx=")
  58.       disp(sig_idx);
  59.       error("sig_idx must be a vector")
  60.     endif
  61.   endif
  62.  
  63.   sig_vals = sysgetsignals(sys,opt);
  64.  
  65.   # make sure it's in state space form if state names are given
  66.   if(strcmp(opt,"st"))    sys = sysupdate(sys,"ss");    endif
  67.  
  68.   if(strcmp(opt,"yd") == 0)
  69.     # it's a signal name list we're changing
  70.     if(!is_list(names))
  71.       names = list(names);
  72.     endif
  73.     if(!is_signal_list(names))
  74.       if(isstr(nth(names,1)))
  75.         warning("syssetsignals(opt=%s): converting string matrix \"names\" to a list of strings",opt);
  76.         tmpstr = nth(names,1);
  77.         for ii=1:rows(tmpstr)
  78.           names(ii) = deblank(tmpstr(ii,:));
  79.         endfor
  80.       else
  81.         names
  82.         error("parameter \"names\" must be a list of strings");
  83.       endif
  84.     endif
  85.     nsigs = length(sig_vals);
  86.  
  87.     if(nargin == 3)
  88.       # replace all signal names
  89.       if(length(names) != nsigs)
  90.         error("opt=%s, sig_idx omitted: names(len=%d) should have %d entries ", ...
  91.           opt,length(names),nsigs);
  92.       endif
  93.       sig_idx = 1:nsigs;
  94.     elseif(length(names) != length(sig_idx))
  95.       # replace specified signal names
  96.       error("opt=%s, sig_idx(len=%d), names(len=%d) mismatch",opt, ...
  97.         length(sig_idx), length(names));
  98.     endif
  99.  
  100.     for ii=1:length(sig_idx)
  101.       jj = sig_idx(ii);
  102.       if(jj < 1 | jj > nsigs | jj != floor(jj+0.5))
  103.         error("opt=%s, sig_idx(%d)=%d, %e: must be an integer between 1 and %d", ...
  104.           opt, ii, jj, jj, nsigs);
  105.       endif
  106.       sig_vals(jj) = nth(names,ii);
  107.     endfor
  108.  
  109.   else
  110.     nsigs = length(sig_vals);
  111.     if(!is_vector(names))
  112.       error("syssetsignals: opt=yd, names(%dx%d) must be a vector", ...
  113.         rows(names), columns(names));
  114.     endif
  115.     if(nargin == 3)
  116.       if(length(names) != nsigs)
  117.         error("opt=yd, sig_idx omitted: names(%d) should be length(%d)", ...
  118.           length(names), nsigs);
  119.       endif
  120.       sig_idx = 1:nsigs;
  121.     elseif(length(names) != length(sig_idx))
  122.       error("opt=yd: length(names)=%d, length(sig_idx)=%d",length(names), ...
  123.         length(sig_idx) );
  124.     endif
  125.  
  126.     badidx = find(names != 0 & names != 1);
  127.     if(! isempty(badidx) )
  128.       for ii=1:length(badidx)
  129.         warning("syssetsignals: opt=yd: names(%d)=%e, must be 0 or 1", ...
  130.           badidx(ii), names(badidx(ii)) );
  131.       endfor
  132.       error("opt=yd: illegal values in names");
  133.     endif
  134.  
  135.     for ii=1:length(sig_idx)
  136.       jj = sig_idx(ii);
  137.       if(jj < 1 | jj > nsigs | jj != floor(jj))
  138.         error("sig_idx(%d)=%d, %e: must be an integer between 1 and %d", ...
  139.           ii,jj, jj, nsigs);
  140.       endif
  141.       sig_vals(jj) = names(ii);
  142.     endfor
  143.     if(any(sig_vals == 1) & sysgettsam(sys) == 0)
  144.       warning("Setting system sampling time to 1");
  145.       printf("syssetsignals: original system sampling time=0 but output(s)\n");
  146.       disp(find(sig_vals==1))
  147.       printf("are digital\n");
  148.       sys = syschtsam(sys,1);
  149.     endif
  150.     
  151.   endif
  152.  
  153.   if(strcmp(opt,"st"))
  154.     sys.stname = sig_vals;
  155.   elseif(strcmp(opt,"in"))
  156.     sys.inname = sig_vals;
  157.   elseif(strcmp(opt,"out"))
  158.     sys.outname = sig_vals;
  159.   elseif(strcmp(opt,"yd"))
  160.     sys.yd = sig_vals;
  161.   endif
  162.  
  163.   retsys = sys;
  164.   implicit_str_to_num_ok = save_val;    # restore value
  165.  
  166. endfunction
  167.