function handlenr=iduiinsm(Model,model_info,model_name,active,axinfo)
%IDUIINSM Handles the insertion of models into the Model Summary Board
%      Model:      The actual model, in theta-format or CRA or SPA model
%      model_info: The asociated model information
%      model_name: The name (a string) of the model.
%      active:     If active==1 then the model should become active immediately
%      HANDLENR:   The handle number of the model pushbutton

%	L. Ljung 4-4-94
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.10 $  $Date: 1995/06/01 10:20:17 $

global  XIDplotw XIDsumb  XIDparest XIDcounters XIDsbmen
XIDcounters(5)=1; 
if isempty(Model)
   mess=str2mat(...
     'WARNING: There were numerical problems to compute the model.',...
     '      The model order might have been too high, or the input',...
     '      signal is not persistently exciting.',...
     '      Use other input or lower model orders.',...
     '      No model inserted.');
   warndlg(mess);
   return
end

if nargin < 4,active=1;end
if active,iduistat('Model being inserted ...');end
if nargin<5
   [axh,texh,linh]=idnextw('model');
else
  [axh,texh,linh]=idnextw('model',axinfo(1),axinfo(2:5),axinfo(6:8));
  if isempty(linh),   [axh,texh,linh]=idnextw('model');end
end

tag=get(axh,'tag');eval('set(XIDparest(3),''userdata'',tag);','')
modnr=eval(tag(6:length(tag)));
theta_model=1;
if strcmp(deblank(model_info(1,:)),'imported')
   T=num2str(gett(Model));
   model_info(2,1:length(T))=T;
   nu=int2str(Model(1,3));
   model_info(6,1:length(nu))=nu;
   if isthss(Model),ny=int2str(Model(1,4));else ny=int2str(1);end
   model_info(3,1:length(ny))=ny;
   nustr=[];nystr=[];
   for ku=1:eval(nu),nustr=[nustr,int2str(ku),' '];end
   for ky=1:eval(ny),nystr=[nystr,int2str(ky),' '];end
   model_info([7],1:length(nystr))=nystr;
   model_info([8],1:length(nustr))=nustr;
end
[ny,nu]=iduiiono('old',model_info,'mod');
[nr,nc]=size(Model);if nc==1,theta_model=0;end
type=model_info(1,1:3);
if type=='cra'|type=='spa'
    theta_model=0;
else 
    if strcmp(get(XIDplotw(3,2),'enable'),'off')
       set(XIDplotw(2:7,2),'enable','on')
    end
end
if type=='cra' 
     set(XIDplotw([5],2),'enable','on'),
     lpl=Model(1:min(20,length(Model(:,1))),1);
elseif type=='spa' 
     set(XIDplotw([2,7],2),'enable','on'),
     lpl=Model(2:length(Model(:,2)),2);
else
    if ~theta_model,Model=iduicalc('unpack',Model);end
    lpl=idsim([[1;zeros(19,1)],zeros(20,max(ny+nu-1,0))],Model,1);
end
lpl=lpl(:,1);

if XIDcounters(3)<nu|XIDcounters(4)<ny
   XIDcounters([3 4])=max(XIDcounters([3 4]),[nu ny]);
   iduiiono('set',XIDcounters([3 4]))
end

set(axh,'vis','off')
set(linh,'UserData',Model,'tag','modelline')
set(texh,'String',model_name,'vis','on')
handlenr=axh;
set(texh,'UserData',model_info)

set(linh,'xdata',1:length(lpl),'ydata',lpl,'vis','off');
set(axh,'ylim',[2*min(lpl)-max(lpl) max(lpl)+eps],'xlim',[1 length(lpl)],...
    'color','k');

if theta_model
 if isthss(Model)
    [par,P,dum]=th2par(Model);ny=Model(1,4);A=th2ss(Model);
    if ~(isempty(P)|norm(P)==0)
       axes(axh)
       text('vis','off','tag','ny0','userdata',length(A)*ny*length(par));
    end
  end
end
if active,
    set(linh,'linewidth',3);
end   
set(linh,'vis','on')
set(axh,'vis','on')

if active,
    Figno=fiactha(XIDplotw(2:7,2))+1;
    iduimod(Figno,modnr,[])
    set(XIDsbmen([3 5]),'enable','on')
    if strcmp(get(XIDsbmen(1),'tag'),'open')
      [label,acc]=menulabel('&Merge session... ^o');
      set(XIDsbmen(1),'label',label,'tag','merge');
    end
    iduistat(['Model ',model_name,' inserted.',...
         '  Double click on icon (right/middle mouse) for text information.'])

end



