function [axh,texh,linh]=idnextw(type,sbnr,pos,col)
%IDNEXTW Finds the next available data or model axes.

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

global XIDsumb XIDcounters

if strcmp(type,'data')
   tag='dataline0';
else
   tag='modelline0';
end

if nargin==4
  create=0;
  eval(['if ~(strcmp(get(XIDsumb(sbnr),''tag''),''sitb30'')',...
        '&get(XIDsumb(sbnr),''userdata'')==sbnr),create=1;end'],...
       'create=1;')
  if create==1&sbnr>1,iddmtab(sbnr);end
  axh=findobj(XIDsumb(sbnr),'type','axes','vis','on','pos',pos);
  if isempty(axh)
     texh=[];linh=[];
  else
     texh=findobj(axh,'tag','name');
     linh=findobj(axh,'tag',tag,'type','line');
     set(linh,'color',col);
  end
else 
ks=1;
stopp=0;
while stopp==0
  create=0;
  if ks==1,tagwin='sitb16';else tagwin='sitb30';end
  eval('if ~strcmp(get(XIDsumb(ks),''tag''),tagwin),create=1;end',...
       'create=1;')
  if create==1,iddmtab(ks);end
  set(XIDsumb(ks),'vis','on')
  hn=findobj(XIDsumb(ks),'type','line','tag',tag);
  if ~isempty(hn), 
     stopp=1;
     kk=1;
     for kh=hn'
        pos=get(get(kh,'parent'),'pos');
        ps(kk)=pos(2)*1000-pos(1);
        kk=kk+1;
     end
     [dum,index]=max(ps);
     linh=hn(index);
     axh=get(linh,'parent');
     texh=findobj(axh,'tag','name');
   end
   ks=ks+1;
end
end
