function handlenr=iduiinsm(Model,active,axinfo,import)
%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 associated 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 1986-2000 The MathWorks, Inc.
%   $Revision: 1.12 $ $Date: 2000/08/24 12:53:19 $

sstat = get(0,'showhiddenhandles');
set(0,'showhiddenhandles','on')
Xsum = findobj(get(0,'children'),'flat','tag','sitb16');
set(0,'showhiddenhandles',sstat)
XID = get(Xsum,'Userdata');
XID.counters(5)=1; 
set(Xsum,'UserData',XID);

if isempty(Model),peflag=1;else peflag=0;end
if peflag
   mess=[...
      'There were numerical problems to compute the model.',...
      '\nThe model order might have been too high, or the input',...
      ' signal is not persistently exciting.',...
      ' Use other input or lower model orders.',...
      '\nNo model inserted.'];
   warndlg(mess);
   return
end
if nargin <4
   import = 0;
end

if nargin < 2,active=1;end
if active,iduistat('Model being inserted ...');end
if nargin<3
   axinfo =[];
end

if isempty(axinfo)
   [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
XID = get(Xsum,'UserData');
tag=get(axh,'tag');
try
set(XID.parest(3),'userdata',tag);
end
modnr=eval(tag(6:length(tag)));
theta_model=1;
 
if import  
   chanupd(Model)
   XID = get(Xsum,'UserData');
end
switch class(Model)
case 'idfrd'
	[ny,nu]=size(Model);
	if nu>0
   set(XID.plotw([2,7],2),'enable','on')
else
	set(XID.plotw(7,2),'enable','on')
end
   mmp=Model;
   lpl = pvget(mmp,'ResponseData');
   if isempty(lpl)
      lpl = pvget(mmp,'SpectrumData');
   end
   lpl = abs(squeeze(lpl(1,1,:)));
   Model_name = pvget(Model,'Name');
otherwise
   if strcmp(get(XID.plotw(3,2),'enable'),'off')
      set(XID.plotw(3:7,2),'enable','on')
   end
   [ny,nu] = size(Model);
   if nu >0
	   set(XID.plotw(2,2),'enable','on')
   end
   if isa(Model,'idmodel')&~(isaimp(Model)|isa(Model,'idpoly'))
      setcov(Model); %%LL%% to do these calc once and for all
   end
   Model_name = pvget(Model,'Name');
   pars  = pvget(Model,'ParameterVector');
   peflag=any(isnan(pars))|any(isinf(pars));
   if peflag
      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
   lpl=sim(Model,[[1;zeros(19,1)],zeros(20,max(ny+nu-1,0))],[],1);
end

lpl=lpl(:,1);
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');
Plotcolors=idlayout('plotcol');
axescolor=Plotcolors(4,:);

ylim = [2*min(lpl)-max(lpl) max(lpl)];
if ylim(1)>=ylim(2),
   ylim = [ylim(1)-1 ylim(2)+1];
end

set(axh,'ylim',ylim,'xlim',[1 length(lpl)],...
   'color',axescolor);

if active,
   set(linh,'linewidth',3);
end   
set(linh,'vis','on')
set(axh,'vis','on')

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