function sb2slgtb(blkname,in,fl,cod,timer)
%SB2SLGTB converts gain table blocks to SIMULINK.
%	SB2SLGTB(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.

%11,6 gain table

% Wes Wang 11/9/92
% Copyright (c) 1990-93 by The MathWorks, Inc.
% $Revision: 1.14 $  $Date: 1993/06/09 22:09:40 $

fb = 1;
if cod(1) == 0
  %continues time system
  fb=2;
  tol = fl(1);
end;
%switch number
swn =  in(in(17)+3);
%vector of switchs
sw=fl(fb:fb+swn-1)';
fb=fb+swn;
%vector of the gain, [g(1); g(2); g(3)...]
for i=1:in(in(17)+3)
  for j=1:in(in(17)+4)
    tmp1 = in(in(17)+4+j);
    tmp2 = in(in(17)+4+j+in(in(17)+4));
    gn((i-1)*in(5)+tmp1, tmp2) = fl(fb);
    fb=fb+1;
  end;
end;

xgn = mat2str(gn); xsw = mat2str(sw);
if length(xgn) >= 255 | length(xsw) >= 255
  load sb2sltmp
  xgn = ['O' num2str(xn+1)];  xsw = ['O' num2str(xn+2)];   
  eval([xgn '= gn;']);
  appsave('sb2sltmp', xgn, gn);
  disp([xgn ' is used as switch brake points in ' blkname])
  eval([xsw '= sw;']);
  appsave('sb2sltmp', xsw, sw);
  disp([xsw ' is used as Gain matrix in ' blkname])  
  xn = xn + 2;
  appsave('sb2sltmp', 'xn', xn);
  %  xs=[xs xgn x sw];
  %  eval(xs);
end;
if swn <= 1
  % it is only a gain block
  add_block('built-in/State-space',[blkname '/Gain'])
  set_param([blkname '/Gain'],...
		'A','[]',...
		'B','[]',...
		'C','[]',...
		'D','G',...
		'Mask Display','G',...
		'Mask Type','Matrix Gain',...
		'Mask Dialogue','Matrix Gain, y = G x|Gain block G:')
  set_param([blkname '/Gain'],...
	'Mask Translate','G=@1',...
	'Mask Help','Matrix gain column equals to output number and row equals input number.',...
	'Mask Entries','z\/',...
	'position',[80,79,120,111])
  set_param([blkname,'/output1'],...
		'position',[150,85,170,105])
  set_param([blkname '/input2'],...
		'Port','2',...
		'position',[30,85,50,105])
  set_param([blkname 'input1'],...
		'position',[30,35,50,55])
  add_line(blkname,[125,95;140,95])
  add_line(blkname,[55,95;70,95])
  set_param(blkname,...
	'Mask Display','G',...
	'Mask Type','Matrix Gain',...
	'Mask Dialogue','Matrix Gain, y = G x|Gain block G:',...
	'Mask Translate','z=@1')
  set_param(blkname,...
   'Mask Help','Matrix gain column equals to output number and row equals input number.The first input is ignored',...
   'Mask Entries',[xgn '\/']);
else
  % it is a full block
  add_block('built-in/State-space',[blkname '/' 'State-space'])
  set_param([blkname '/' 'State-space'],...
		'A','[]',...
		'B','[]',...
		'C','[]',...
		'D','G',...
		'position',[280,80,340,110]);
  for i=2:in(4)
    set_param([blkname '/input' num2str(i)], 'Port', num2str(i), ...
      'position',[70+rem(i-2,3)*30,40+i*15,90+rem(i-2,3)*30,60+i*15])
  end;
  if in(4) > 2
    add_block('built-in/Mux',[blkname,'/','Mux'])
    set_param([blkname,'/','Mux'],...
		'inputs',num2str(in(4)-1),...
		'position',[225,69,255,121])
    for i= 2:in(4)
      autoline(blkname,['input' num2str(i) '/1'],['Mux/' num2str(i-1)]);
    end;
    autoline(blkname,'Mux/1','State-space/1');
  else
    autoline(blkname, 'input2/1','State-space/1');
  end;
  add_block('built-in/Demux',[blkname '/' 'Demux'])
  tmp = mat2str(ones(1,swn)*in(5));
  set_param([blkname,'/','Demux'],...
		'orientation',2,...
		'outputs',tmp,...
		'position',[440,100,475,swn*10+110])
  % add part to avoid a bug
  tmp = get_param([blkname,'/','Demux'],'position');
  set_param([blkname,'/','Demux'],'position',tmp);
  autoline(blkname,'State-space/1','Demux/1',[],[0,2]);
  add_block('built-in/Demux',[blkname '/Demux1'])
  set_param([blkname '/Demux1'],'outputs',num2str(in(5)),...
 	'position',[570,60,605,80+10*in(5)]);
  % add part to avoid a bug
  tmp = get_param([blkname,'/','Demux1'],'position');
  set_param([blkname,'/','Demux1'],'position',tmp);
  for i=1:in(5)
    %get all of the output done
    set_param([blkname '/output' num2str(i)],...
       'Port',num2str(i),...
       'position',[690-rem(i-1,3)*30,50+i*15, 710-rem(i-1,3)*30 70+i*15]);
    autoline(blkname, ['Demux1/' num2str(i)],['output' num2str(i) '/1']);
  end;
end;

x=max(swn*10+100,75+i*15) + 40;
set_param([blkname '/input1'], 'position',...
   [110,x+15,130,x+35]);
for i = 1 : swn-1
  % place switchs,mux and fcn
  add_block('built-in/Fcn',[blkname '/Fcn' num2str(i)]);
  set_param([blkname '/Fcn' num2str(i)],...
     'Expr',['x(' num2str(i+1) ')-u'],...
     'position',[185,x+90*(swn-i-1)+10,250,x+90*(swn-i-1)+40]);
  add_block('built-in/Switch',[blkname,'/Switch' num2str(i)])
  set_param([blkname '/Switch' num2str(i)],...
     'position',[500,x+90*(swn-i-1)+5,525,x+90*(swn-i-1)+45]);
  autoline(blkname, 'input1/1', ['Fcn' num2str(i) '/1']);
  if in(5) > 1
    add_block('built-in/Mux',[blkname,'/','Mux' num2str(i)])
    set_param([blkname,'/','Mux' num2str(i)],...
       'inputs',num2str(in(5)),...
       'position',[310,x+90*(swn-i-1),340,x+90*(swn-i-1)+50]);
    for j=1:in(5)
      autoline(blkname, ['Fcn' num2str(i) '/1'], ['Mux' num2str(i) '/' num2str(j)]);
    end;
    autoline(blkname, ['Mux' num2str(i) '/1'], ['Switch' num2str(i) '/2']);
  else
    autoline(blkname,['Fcn' num2str(i) '/1'],['Switch' num2str(i) '/2']);
  end
  autoline(blkname, ['Demux/' num2str(i+1)], ['Switch' num2str(i) '/1'],[],[2 0]);
  if i== 1
    autoline(blkname, 'Demux/1', 'Switch1/3',[],[2 0]);
  else
    autoline(blkname, ['Switch' num2str(i-1) '/1'], ['Switch' num2str(i) '/3']);
  end;
end;
autoline(blkname, ['Switch' num2str(swn-1) '/1'], 'Demux1/1');
set_param(blkname,'Mask Type','Gain Table','Mask Translate','x=@1; G=@2;')
set_param(blkname,'Mask Entries',[xsw '\/' xgn '\/'])
temp = 'Gain schedule\ny=G(i)*u2 when p(i-1)<u1<p(i).|Switch break-points [p1...pn]:|Gain matrices [G(1);...;G(n)]:';
set_param(blkname,'Mask Dialogue',temp);
temp = 'plot(0,0,10,10,[0,8,8 7.7 8.3 8],[9 9 5.5 6.5 6.5 5.5],';
temp = [temp '[0 2 2 5 5 2 2 5 5 7 9 10],[7 7 7.5 7.5 6.5 6.5 7.5 7.5 7 7 4 4],'];
temp = [temp '[1 1 2 2 5 5 2 2 5 5 7],[7 1.5 1.5 2 2 1 1 2 2 1.5 1.5],'];
temp = [temp '[1 2 2 5 5 2 2 5 5 7],[5 5 5.5 5.5 4.5 4.5 5.5 5.5 5 5])'];
set_param(blkname,'Mask Display', temp);
temp = 'The first input is a scalar switch variable which choose the ';
temp = [temp 'appropriate Gain matrix. The second input is a vector which '];
temp = [temp 'is multiplied by the appropriate gain matrix to produce '];
temp = [temp 'the output:\nu1<p2,      output = G1*u2;'];
temp = [temp            '\np2<= u1<p3, output = G2*u2;\n...'];
temp = [temp            '\npn<=u[1],   output = G2*u2.'];
set_param(blkname,'Mask Help',temp,...
   'Mask Entries',[xsw '\/' xgn '\/'])

