function iddatfig(datas,figures)
%IDDATFIG Generates all view curves for ident data objects.
%       The function generates all curves associated with models DATAS 
%       in viewwindows FIGURES. The handle numbers of these curves are
%       stored in the userdata of the corresponding AXES, so that row
%       number 2*K+1 of the userdata contains the handles of lines
%       associated with model number K, 

%	L. Ljung 4-4-94
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.7 $  $Date: 1995/05/10 21:36:59 $

global   XIDplotw  XIDsumb  XIDfilt XIDcounters
nnu=XIDcounters(3);
sumbs=findobj(get(0,'children'),'flat','tag','sitb30');
modaxs=get(XIDsumb(1),'children');
for kk=sumbs(:)'
   modaxs=[modaxs;get(kk,'children')];
end
for Figno=figures
  figure(XIDplotw(Figno,1)),
  xusd=get(XIDplotw(Figno,1),'Userdata');
  xax=xusd(3:length(xusd));
  if isempty(get(xax(1),'children')),newplot=1;else newplot=0;end
  Opthand=XIDplotw(Figno,2);
  opt=get(Opthand,'UserData');
  hnr=findobj(XIDplotw(Figno,1),'label',menulabel('Erasemode &xor'));
  if strcmp(get(hnr,'checked'),'on') 
     ermode='xor';
  else 
     ermode='normal';
  end
%ermode='normal';
  [kydes,kudes]=iduiiono('unpack',Figno);
  if any(Figno==[1,14])   % This is the time plot case
     iduistat('Preparing the time plots ...')
     stairplot=eval(deblank(opt(1,:)));
      
      for k=datas
       doplot=1;docalc=1;
       if k==0
         [data,data_info,data_n,hand]=iduigetd('e');
         khax=hand(1);klin=hand(2);kstr=hand(3);
         kn=k+2;
       else    
         khax=findobj(modaxs,'flat','tag',['data ',int2str(k)]);
         kstr=findobj(khax,'tag','name');
         klin=findobj(khax,'tag','dataline');
         data_info=get(kstr,'userdata');
         data_n=get(kstr,'string');
         data=get(klin,'userdata');
         kn=k;
       end
       tSamp=eval(data_info(1,:));dLen=length(data);
       t0=eval(data_info(3,:));
       [ny,nu,ky,ku]=iduiiono('old',data_info,'dat',[kudes,kydes]);
       if ku==0,ku=[];end
       if Figno==14,nnu=nu;end
       doplot=1;
       if isempty(ky)|isempty(ku)
          iduistat('Data set(s) incompatible with chosen channels.',0,Figno);
          doplot=0;  figure(XIDplotw(Figno,1)),
       end
       if doplot
         axes(xax(1))
         color=get(klin,'color');
         xusd1=get(xax(1),'userdata');
         xusd1(2*kn+1,1)=line(t0+tSamp*[0:dLen-1],data(:,ky),'color',color,...
                  'erasemode',ermode,'userdata',kstr);
	 set(xax(1),'vis','on'),vy=axis;
         set(xax(1),'userdata',xusd1);
         plha=xusd1(2*kn+1,1);
         if Figno==1,
            pos = idlayout('axes',1);posy =pos(1,:);
	    posyts = idlayout('axes',7);
         else
            pos = idlayout('axes',14); posy = pos(1,:);
	    posyts = idlayout('axes',3);
         end
         if ~isempty(ku)
           
             set(xax(1),'pos',posy,'xticklabels',[]);
             axes(xax(2))
             xusd2=get(xax(2),'userdata');
	     xs=t0+tSamp*[0:dLen-1];ys=data(:,ny+ku);
	     if stairplot==1
	     	[xs,ys]=stairs(xs,ys);
	     end
	     xusd2(2*kn+1,1)=line(xs,ys,'color',color,'erasemode',ermode,...
                     'userdata',kstr);
%	     v=axis;axis([vy(1) vy(2) 1.1*v(3) 1.1*v(4)]);
	     set(xax(2),'vis','on','userdata',xusd2)
             haxe=[xax(1) xax(2)];
	     plha=[plha,xusd2(2*kn+1,1)];
          elseif nnu==0
	     set(xax(1),'pos',posyts,'xticklabelmode','auto');
             axes(xax(2)),cla,
             set(xax(2),'vis','off')
          end % if isempty(ku)
           if Figno==1,
             usd=[idnonzer(get(khax,'userdata'));plha(:)];
             set(khax,'userdata',usd);
           end
         end % if doplot
       end  % for k=dates
   elseif any(Figno==[13,15])  % This is the spectral case
     iduistat('Preparing the time plots ...')
     isfft=eval(deblank(opt(5,:)))-1;
     W=eval(deblank(opt(4,:)));
     hz=eval(opt(3,:))-1;
     plx=eval(opt(1,:))-1;
     ply=eval(opt(2,:))-1;
     for k=datas   % k=-1 means filtered data in plot 15, k=0 is current data
         doplot=1;docalc=1;
       if any(k==[0,-1])
         [data,data_info,data_n,hand]=iduigetd('e');
         khax=hand(1);klin=hand(2);kstr=hand(3);
         if k==-1,
            khax=XIDfilt(1,1); 
            data=get(khax,'userdata');
            if isempty(data),return,end
         end
         kn=k+2;
       else    
         khax=findobj(modaxs,'flat','tag',['data ',int2str(k)]);
         kstr=findobj(khax,'tag','name');
         klin=findobj(khax,'tag','dataline');
         data_info=get(kstr,'userdata');
         data_n=get(kstr,'string');
         data=get(klin,'userdata');
         kn=k;
       end
	 tSamp=eval(data_info(1,:));dl=length(data);
	 t0=eval(data_info(3,:));
        [ny,nu,ky,ku]=iduiiono('old',data_info,'dat',[kudes,kydes]);
        if ku==0,ku=[];end
        if Figno==15,nnu=nu;end
        doplot=1;
       if isempty(ky)|isempty(ku)
          iduistat('Data set(s) incompatible with chosen channels.',0,Figno);
          doplot=0;  figure(XIDplotw(Figno,1)),

       end
       if doplot

     %Compute spectra

     iduistat('Performing spectral analysis ...')

     if ~isfft
	  usd=get(XIDplotw(16,1),'UserData');
         OPt=usd([2,3],:);
         TYpe=eval((OPt(2,:)));
         MSpa=eval((OPt(1,:)));
	  if isempty(W)
	     LW=128;
	  else 
	     LW=2^nextpow2(W); 
	  end
	  if isempty(MSpa)
	     MSpa=30;
	  end 
	  if TYpe==2  % This is ETFE
	     if nu>0
		 gu=etfe(data(:,[ny+ku]),MSpa,LW,tSamp);
		 [ggwu,ggau]=getff(gu);
	     end
	     gy=etfe(data(:,ky),MSpa,LW,tSamp);

	  else     % This is SPA
	     gy=spa(data(:,ky),MSpa,W,[],tSamp);
	     if nu>0
		gu=spa(data(:,ny+ku),MSpa,W,[],tSamp);
		[ggwu,ggau]=getff(gu);
	      end
	  end % if Type
   	  [ggwy,ggay]=getff(gy);

     else %  is fft , i.e. we do fft

          ggwy=[0:dl-1]/dl*2*pi/tSamp;kmax=floor(dl/2);
          ggwy=ggwy(1:kmax+1)';ggwu=ggwy;
          yf=fft(data(:,ky),dl);ggay=abs(yf(1:kmax+1)).^2*tSamp/dl;
          if nu>0,
             uf=fft(data(:,ny+ku),dl);ggau=abs(uf(1:kmax+1)).^2*tSamp/dl;
          end
     end  % if isfft


     % Plot the result
     if hz ggwy=ggwy/2/pi;hztex=' (hz)';else hztex=' (rad/s)';end
     if plx, set(xax,'Xscale','log'),else set(xax,'Xscale','linear'),end
     if ply, set(xax,'Yscale','log'),else set(xax,'Yscale','linear'),end
     if k==-1,
        [axh,texh,linh]=idnextw('data');color=get(linh,'color');
     else 
        color=get(klin,'color');
     end
     newbox1=0;newbox2=0;
     if k==0, 
        set(XIDfilt(5,1),'string',['Range',hztex]);
        hb=get(XIDfilt(2,1),'userdata');newbox1=1;newbox2=1;
        if length(hb)>0,if ishandle(hb(1)),newbox1=0;end,end
        if length(hb)>1,if ishandle(hb(2)),newbox2=0;end,end
     end
     axes(xax(1));xusd1=get(xax(1),'userdata');
     if k==-1
            eval('delete(xusd1(7,1))','')
	    eval('xusd1(7,1)=xusd1(3,1);','')
     end
     xusd1(2*kn+1,1)=line(ggwy,ggay,'color',color,'erasemode',ermode,...
                 'userdata',kstr);
     if newbox1, hb(1)=line('color','g','linestyle','--','erasemode','xor',...
             'vis','off');
     end
     set(xax(1),'vis','on','userdata',xusd1)
     handpl=xusd1(2*kn+1,1);
     if k==-1,hnrf=[xusd1(3,1),xusd1(7,1)];end
     if Figno==13,
            pos = idlayout('axes',1);posy =pos(1,:);
	    posyts = idlayout('axes',7);
     else
            pos = idlayout('axes',14); posy = pos(1,:);
	    posyts = idlayout('axes',3);
     end
     if ~isempty(ku)
         set(xax(1),'pos',posy,'xticklabels',[]);
         axes(xax(2));xusd2=get(xax(2),'userdata');
         if k==-1
            eval('delete(xusd2(7,1))','')
	    eval('xusd2(7,1)=xusd2(3,1);','xusd2(7,1)=-1;')
         end
         if hz ggwu=ggwu/2/pi;end
         xusd2(2*kn+1,1)=line(ggwu,ggau,'color',color,'erasemode',ermode,...
                   'userdata',kstr);
         if newbox2,
               hb(2)=line('color','g','linestyle','--','erasemode','xor',...
                 'vis','off');
         end
         set(xax(2),'vis','on','userdata',xusd2)
         haxe=[xax(1) xax(2)];
         if k==-1,hnrf=[hnrf,xusd2(3,1),xusd2(7,1)];end
         handpl=[handpl,xusd2(2*kn+1,1)];
      elseif nnu==0 % i.e. nu=0
         set(xax(1),'pos',posyts,'xticklabelmode','auto');
         axes(xax(2)),cla,
         set(xax(2),'vis','off');if k==0,hb=hb(1);end
     end  % if nu
     if k>0,usd=[idnonzer(get(khax,'userdata'));handpl(:)];
     set(khax,'userdata',usd);end
     if k==-1,set(XIDfilt(3,1),'userdata',hnrf);end
     if k==0,
        set(XIDfilt(2,1),'userdata',hb);
     end
    end % if doplot
   end  % for k=
  end  % if Figno
end % for Figno=
iduital(Figno);
iduistat('')
