function idinseva(ax1,tag2,silent)
%IDINSEVA Inserts the working or validation data set.
%   The data information in ax1 is placed as working data
%   (tag2='seles') or validation data (tag2='selva');
%   if silent==1, no status messages are displayed

%   L. Ljung 9-27-94
%   Copyright 1986-2000 The MathWorks, Inc.
%   $Revision: 1.9 $ $Date: 2000/08/24 12:53:26 $

Xsum = findobj(get(0,'children'),'flat','tag','sitb16');
XID = get(Xsum,'Userdata');

if nargin<3,silent=0;end
if strcmp(tag2,'seles'),
   ax2=XID.hw(3,1);
else 
   ax2=XID.hw(4,1);
end
oldusd = get(ax2,'userdata');
par1=get(ax1,'parent');par2=get(ax2,'parent');
tag1=get(ax1,'tag');
oldtag=get(get(ax2,'zlabel'),'userdata');
if strcmp(tag1,oldtag)
   if strcmp(tag2,'seles')
      dtype='Working ';
   else
      dtype='Validation ';
   end
   iduistat([dtype,'data unchanged. No update necassary.'])
   return
end
pos2=get(ax2,'pos');pos1=get(ax1,'pos');
usd2=get(ax2,'userdata');
if isempty(usd2),
   usd2.ny = 0;usd2.nu=0;
end
hnrstring2=findobj(ax2,'type','text','tag','name');
hnrline2=findobj(ax2,'tag','selline');
hnrstring1=findobj(ax1,'type','text','tag','name');
hnrline1=findobj(ax1,'tag','dataline');
set(get(ax2,'zlabel'),'userdata',tag1); % Tagging the working/validation
% data with a name tag
set(hnrstring2,'string',get(hnrstring1,'string'),'userdata',...
   get(hnrstring1,'userdata'),'vis','on')
data = get(hnrline1,'userdata');
data_n = pvget(data,'Name');
set(hnrline2,'xdata',get(hnrline1,'xdata'),...
   'ydata',get(hnrline1,'ydata'),'vis','on',...
   'userdata',data);
 [N,ny,nu]=size(data);
newusd.ny = ny;
newusd.nu = nu;
newusd.ynames = pvget(data,'OutputName');
newusd.unames = pvget(data,'InputName');
uynames ={};
for kkk = 1:length(newusd.unames)
   uynames{kkk} = newusd.ynames;
end

newusd.uynames = uynames;

%newusd=[nu,ny,nustr,nystr]; %%LL%%??
set(ax2,'xlim',get(ax1,'xlim'),...
   'ylim',get(ax1,'ylim'),'userdata',newusd);
newax=0;
if strcmp(tag2(1:5),'selva')
   Plotcolors=idlayout('plotcol');
   textcolor=Plotcolors(6,:);
   set(hnrline2,'color',textcolor);
   if iduiwok(3)
      hpred=findobj(XID.plotw(3,1),'tag','predict');
      hsim=findobj(XID.plotw(3,1),'tag','simul');
      if nu==0,
         if strcmp(get(hsim,'checked'),'on')
            idopttog('check',hpred,0);
         end
         set(hsim,'enable','off');
      else
         set(hsim,'enable','on');
      end
   end
   sampp=get(XID.plotw(3,2),'userdata');
   sampp=str2mat(sampp(1,:),sampp(2,:),'[]',sampp(4,:));
   set(XID.plotw(3,2),'userdata',sampp);
  
   iduiiono('set',newusd,[3 6]);
   iduiclpw(3);
   iduiclpw(6);
   if ~silent
      iduistat(['Validation data changed to ',data_n'.'])
   end
else
   set(hnrline2,'color',get(hnrline1,'color'));
    ll1=iduiwok(35);if ishandle(ll1),if strcmp(get(ll1,'vis'),'on')
         iduisel('revert_exp');
      end,end

   ll1=iduiwok(28);if ishandle(ll1),if strcmp(get(ll1,'vis'),'on')
         iduisel('revert_io');
      end,end
   ll1=iduiwok(30);if ishandle(ll1),if strcmp(get(ll1,'vis'),'on')
         iduisel('revert_dec');
      end,end
   ll1=iduiwok(14);if ishandle(ll1),if strcmp(get(ll1,'vis'),'on')
         iduisel('open_portions');
      end,end
   ll1=iduiwok(15);if ishandle(ll1),if strcmp(get(ll1,'vis'),'on')
         iduifilt('open');
      end,end
   
   
   if nu~=usd2.nu|ny~=usd2.ny
      ll1=iduiwok(20);
      if ishandle(ll1),
         if strcmp(get(ll1,'vis'),'on')
            idparest('open');
         end,
      end
   else
      if nu>0,
         if ~allstr(newusd.unames,usd2.unames)|~allstr(newusd.ynames,usd2.ynames)%(nustr==usd2.unames)|~all(nystr==usd2.ynames)
            ll1=iduiwok(21);
            if ishandle(ll1),
               if strcmp(get(ll1,'vis'),'on')
                  iduiio('open');
               end,
            end
         end
      else
         if ~allstr(newusd.ynames,usd2.ynames)%(nystr==usd2(3+nu:ny+nu+2))
            ll1=iduiwok(21);
            if ishandle(ll1),
               if strcmp(get(ll1,'vis'),'on')
                  iduiio('open');
               end,
            end
         end
      end
      
   end
   if ~silent
      iduistat(['Working data changed to ',data_n '.'])
   end
end

function ok = allstr(str1,str2)
ok = 1;
if length(str1)~=length(str2)
   ok = 0;
   return
end
for k = 1:length(str1)
   if ~any(strcmp(str1{k},str2))
      ok = 0;
      break
   end
end


   
