function sb2sltf(blkname,in,fl,cod,timer)
%SB2SLTF converts transfer function blocks into SIMULINK.
%	SB2SLTF(BLKNAME,IN,FL,COD,TIMER) converts a SystemBuild block
%       to a SIMULINK block.  BLKNAME is a string containing the name of
%       the block. The parameters are set up for this block based on the
%       SystemBuild file coding integer IN and real number FL.
%
%       COD is a three dimensional vector. The first element indicates
%       whether it is a continuous (0) or discrete (1) time system.
%       The second element indicates whether it does not need (0) or
%       does need (1) a mask. The third element indicates the
%       orientation of the block.
%
%       TIMER is designed for discrete time only. The first element of
%       TIMER is the sampling time. The second element is the initial time.

% Wes Wang 10/2/92 -- 10/6/92
% Copyright (c) 1990-93 by The MathWorks, Inc.
% $Revision: 1.11 $  $Date: 1993/06/09 22:09:40 $

% in = [8, code1 code2, input, output, state,....
% state / output --> order of integer for each one
% if input > output --> resettable
%   (input - 1)/output - 1 = highest order resettable   
% this one works the same for continuous or discrete time systems
%
% real part: initial condition + gain
% if gain ~= 1 add a gain block
%
% in(in(17)+3) --> order of denominator
% in(in(17)+4) --> order of numerator
% fl(1:in(in(17)+3)+1) --> parameters of denominator
% numerator could be single input/multi output in SIMULINK
% k=in(in(17)+3)+1
% for l=1:in(4) %input number
%   for i=1:in(in(17)+4)+1 %number of numerator parameter
%     for j=1:in(5) %output number
%       k = k+1;
%       p(j,i)=fl(k);
%     end;
%   end;
%   set parameter for p
% end;
% 

%test --> need superblock for any reason
% if output==input==1 then we don't need a group.
if in(4) == 1 & in(5) == 1
  test = 0;
else
  test = 1;
end;

%read in all of denominator and numerators
%read denominator
k=in(in(17)+3)+1;
den = fl(1:k)';
%read numerator
%num(:,(i-1)*(in(in(17)+4)+1)) is ith input numerator
for l=1:in(4) %input number
  for j=1:in(5) %output number
    for i=1:in(in(17)+4)+1 %number of numerator parameter
      k = k+1;
      num(j,(l-1)*(in(in(17)+4)+1)+i)=fl(k);
    end;
  end;
end;

if ~test
  set_param(blkname,'Numerator',mat2str(num),...
	'Denominator',mat2str(den));
  if cod(1) ~= 0
    set_param(blkname,'Sample time',num2str(timer(1)))
  end;
else
  set_param(blkname,'Location',[290,296,842,606])
  for i=1:in(4)
  %for each input
    if cod(1) ==0
      add_block('built-in/Transfer Fcn',[blkname '/Trans_Fcn' num2str(i)]);
    else
      add_block('built-in/Discrete Transfer Fcn',...
	[blkname '/Trans_Fcn' num2str(i)]);
    end;
    set_param([blkname '/Trans_Fcn' num2str(i)],'Numerator',...
      mat2str(num(:,(i-1)*(in(in(17)+4)+1)+1:i*(in(in(17)+4)+1))),...
      'Denominator',mat2str(den),...
      'position',[85,(70+in(5))*i-15,195,(70+in(5))*i+25]);
    if cod(1) ~= 0
      set_param([blkname '/Trans_Fcn' num2str(i)],...
	'Sample time',num2str(timer(1)))
    end;
    set_param([blkname '/input' num2str(i)], 'Port', num2str(i),...
      'position', [15,(70+in(5))*i-5,35,(70+in(5))*i+15]);
    autoline(blkname,['input' num2str(i) '/1'],...
      ['Trans_Fcn' num2str(i) '/1']);
    if in(5) >1
      add_block('built-in/Demux',[blkname,'/Demux' num2str(i)]);
      set_param([blkname,'/Demux' num2str(i)],'outputs',num2str(in(5)),...
        'position',[245,(70+in(5))*i+5-5*in(5),285,(70+in(5))*i+5+5*in(5)]);
      tmp = get_param([blkname '/Demux' num2str(i)],'position');
      set_param([blkname '/Demux' num2str(i)],'position',tmp);
      autoline(blkname,['Trans_Fcn' num2str(i) '/1'],...
	['Demux' num2str(i) '/1']);
	% [175,(50+in(5))*i+5;245,(50+in(5))*i+5]);
    end;
  end;
  if in(5) > 1
    fn='Demux';
  else
    fn='Trans_Fcn';
  end;
  if in(4) >1
    signn = [];  for i=1:in(4),  signn = [signn '+']; end;
    for i=1:in(5),
      add_block('built-in/Sum',[blkname '/Sum' num2str(i)])
      set_param([blkname '/Sum' num2str(i)],'inputs',signn,...
        'position',[385,(70+in(4))*i+5-5*in(4),405,(70+in(4))*i+5+5*in(4)]);
      for j=1:in(4),
        autoline(blkname,[fn num2str(j) '/' num2str(i)],...
          ['Sum' num2str(i) '/' num2str(j)]);
      end;
      set_param([blkname '/output' num2str(i)],...
        'Port',num2str(i),...
        'position',[450,(70+in(5))*i-5,470,(70+in(5))*i+15]);
      autoline(blkname,['Sum' num2str(i) '/1'],['output' num2str(i) '/1']);
      %     405,(50+in(5))*i+5;450,(50+in(5))*i+5]);
    end;
  else
    for i=1:in(5)
      set_param([blkname '/output' num2str(i)],...
        'Port',num2str(i),...
        'position',...
        [355+rem(i,2)*30,60-4*in(5)+i*10,375+rem(i,2)*30,80-4*in(5)+i*10]);
      autoline(blkname,[fn '1/' num2str(i)],['output' num2str(i) '/1']);
    end;
  end;
  if cod(1) == 0
    set_param(blkname,'Mask Display','num(s)\n-----\nden(s)');
  else
    set_param(blkname,'Mask Display','num(z)\n-----\nden(z)');
  end;
end;

