function outarg=iduifile(arg,file,dum)
%IDUIFILE Handles file operations in ident.
%   Arguments:
%   load_data   Opens the dialog box for importing datavariable
%   load_iodata Opens the dialog box for importing data in i/o form
%   load_model  Opens the dialog box for importing models
%   load_mat    Loads the selected mat file
%   done_data   Final operations when inserting data
%   reset       Resets the dialog's different boxes
%   insert      Inserts i/o data into summary board
%   test1       Tests if sampling interval reasonable
%   This function works in symbiosis with the script file IDUIFSCR that 
%   does some of the operation in the workspace.

%   L. Ljung 4-4-94
%   Copyright 1986-2000 The MathWorks, Inc.
%   $Revision: 1.15 $  $Date: 2000/09/01 13:41:18 $


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

%iduistat('') % Removes Compiling... message
if strcmp(arg,'load_iodata')
   cb1='iduipoin(1);iduistat(''Compiling...'');';
   cb2='iduipoin(1);';
   cb3='iduipoin(2);';
   FigName=idlaytab('figname',19);
   if ~figflag(FigName,0)
      iduistat('Opening the import dialog box ...')
      layout
      butw=mStdButtonWidth;buth=mStdButtonHeight;
      FigW=iduilay2(2);
      f=figure('vis','off',...
         'NumberTitle','off','Name',FigName,...
         'Integerhandle','off','HandleVisibility','callback',...
         'Color',get(0,'DefaultUIControlBackgroundColor'),'tag','sitb19');
      set(f,'Menubar','none');
      strload=[];
      XID.hload(1,1) = f;
      % LEVEL 1
      
      pos = iduilay1(FigW,4,2);
      uicontrol(f,'pos',pos(1,:),'style','frame');
      uicontrol(f,'pos',pos(2,:),...
         'style','push','string','Import','callback',...
         [cb1,'iduifile(''insert'');',cb3]);
      uicontrol(f,'pos',pos(3,:),'style',...
         'push','string','Reset','callback','iduifile(''reset'',1)');
      uicontrol(f,'pos',pos(4,:),...
         'style','push','string','Close',...
         'callback','set(gcf,''vis'',''off'')');
      uicontrol(f,'pos',pos(5,:),...
         'style','push','string','Help',...
         'callback','iduihelp(''iduiimp2.hlp'',''Importing Data'');');
      pos1 = pos;
      
      % LEVEL 1.5 Frame for data type
      %lev15 = pos(1,2)+pos(1,4);
      pos2 = iduilay1(FigW,4,4,4,2);%,lev15,4);
      
      uicontrol(f,'pos',pos2(1,:)+[FigW 0 0  0],'style','Frame');
      XID.hload(1,12) = uicontrol(f,'pos',...
         pos2(5,:)+[FigW 0 0 2.4*buth],...
         'style','edit',...
         'HorizontalAlignment','left',...
         'String','','max',2,'Backgroundcolor','w',...
         'Tooltip',['Enter here any notes that will follow the' ...
            ' data.']);
      uicontrol(f,'pos',pos2(2,:)+[FigW 0 0 0],'style','text','HorizontalAlignment',...
         'left','string','Notes','Fontweight',...
         'bold');
      levright = pos2(1,2)+pos2(1,4);
      % LEVEL 2 Frame for Data Info 
      lev2=pos(1,2)+pos(1,4);
      pos = iduilay1(FigW,10,5,lev2,4,[2 2]/2);
      pos2 = iduilay1(FigW,10,5,lev2,4,[1 3]/2);
      pos2 = pos2+ones(size(pos2,1),1)*(FigW*[1 0 0 0]);
      
      uicontrol(f,'pos',pos(1,:),'style','frame');
      posright = iduilay1(FigW,3,3,levright);
      uicontrol(f,'pos',posright(1,:)+[FigW 0 0 0],'style','frame');
      
      %uicontrol(f,'pos',...
      %        pos(10,:),'style','text',...
      %         'HorizontalAlignment','left',...
      %        'string','Notes:');
      XID.hload(1,14) =     uicontrol(f,'pos',pos(11,:),'style','push',...
         'string','More','callback','iduifile(''more'')');
      
      XID.hload(1,17) = uicontrol(f,'pos',pos2(7,:),...
         'Backgroundcolor','w',...
         'HorizontalAlignment','left','style','Edit','string',' ');
      uicontrol(f,'pos',pos2(6,:),'style','text',...
         'HorizontalAlignment','left','string','Output:');
      XID.hload(1,18) = uicontrol(f,'pos',pos2(5,:),...
         'Backgroundcolor','w',...
         'HorizontalAlignment','left','style','Edit','string',' ');
      uicontrol(f,'pos',pos2(4,:),'style','text',...
         'HorizontalAlignment','left','string', 'Input:');
      
      
      uicontrol(f,'pos',pos(4,:),'style',...
         'text','string','Data name:',...
         'HorizontalAlignment','left');
      XID.hload(1,4) = uicontrol(f,'pos',pos(5,:),'style','edit',...
         'HorizontalAlignment','left',...
         'String','mydata','Backgroundcolor','w');
      uicontrol(f,'pos',pos(6,:),...
         'String','Starting time:','style','text',...
         'horizontalalignment','left');
      XID.hload(1,8) = uicontrol(f,'pos',pos(7,:),...
         'style','edit','String','1','tag','startt',...
         'HorizontalAlignment','left','callback','iduifile(''test1'');',...
         'Backgroundcolor','w');
      uicontrol(f,'pos',pos(8,:),...
         'String','Samp. interv.:','style','text',...
         'horizontalalignment','left');
      XID.hload(1,6) = uicontrol(f,'pos',pos(9,:),'tag','tsamp',...
         'HorizontalAlignment','left','callback','iduifile(''test1'');',...
         'style','edit','String','1','Backgroundcolor','w');
      uicontrol(f,'pos',[pos(2,1:2) ...
            FigW-2*(mEdgeToFrame+mFrameToText) pos(2,4)],...
         'style','text','string','Data Information',...
         'HorizontalAlignment','center','fontweight','bold');
      uicontrol(f,'pos',[pos(2,1:2) ...
            FigW-2*(mEdgeToFrame+mFrameToText) pos(2,4)]+[FigW 0 0 0],...
         'style','text','string','Physical Units of Variables ',...
         'HorizontalAlignment','center','fontweight','bold');
      
      
      % LEVEL 3 Frame for Variable info
      
      lev3=pos(1,2)+pos(1,4);
      
      pos = iduilay1(FigW,6,3,lev3,4,[1 3]/2);
      uicontrol(f,'pos',pos(1,:),'style','frame');
      uicontrol(f,'pos',pos(2,:)+[0 0 FigW-pos(2,3)-2*(mEdgeToFrame+mFrameToText) 0],'style','text','horizontalalignment','center','string',...
         'Workspace Variable','fontweight','bold');
      XID.hload(1,24) = uicontrol(f,'pos',pos(4,:),...
         'HorizontalAlignment','left',...
         'style','text','string','Input:');
      XID.hload(1,11)=...
         uicontrol(f,'pos',pos(5,:),...
         'style','edit','Backgroundcolor','w',...
         'HorizontalAlignment','left','callback',...
         'iduifile(''iddata'');','tooltip',...
         ['Any MATLAB expression that evaluates to an N-by-Nu matrix']);
      XID.hload(1,25) = uicontrol(f,'pos',pos(6,:),...
         'style','text','string','Output:',...
         'HorizontalAlignment','left');
      XID.hload(1,13)=...
         uicontrol(f,'pos',pos(7,:),...
         'style','edit','Backgroundcolor','w',...
         'HorizontalAlignment','left','tooltip',...
         ['Any MATLAB expression that evaluates to an N-by-Ny matrix']);
      
      uicontrol(f,'pos',pos(1,:)+[FigW 0 0 0],'style','frame');
      uicontrol(f,'pos',pos(4,:)+[FigW 0 0 0],...
         'HorizontalAlignment','left',...
         'style','text','string','Input:')
      XID.hload(1,19)=...
         uicontrol(f,'pos',pos(5,:)+[FigW 0 0 0],...
         'style','edit','Backgroundcolor','w',...
         'HorizontalAlignment','left','tooltip',...
         ['Enter channel names as strings without quotes, comma separated']);
      uicontrol(f,'pos',pos(6,:)+[FigW 0 0 0],...
         'style','text','string','Output:',...
         'HorizontalAlignment','left');
      XID.hload(1,20)=...
         uicontrol(f,'pos',pos(7,:)+[FigW 0 0 0],...
         'style','edit','Backgroundcolor','w',...
         'HorizontalAlignment','left','tooltip',...
         ['Enter channel names as strings without quotes, comma separated']);
      
      uicontrol(f,'pos',...
         [pos(2,1:2) FigW-2*(mEdgeToFrame+mFrameToText) pos(2,4)]+[FigW 0 0 0],...
         'style','text','string',...
         'Channel Names','fontweight','bold');
      %%% TOP LEVEL
      lev3=pos(1,2)+pos(1,4);
      
      pos = iduilay1(FigW,3,3,lev3,4);
      uicontrol(f,'style','frame','pos',pos(1,:));
      XID.hload(1,21) = uicontrol(f,'pos',...
         pos(3,:)+[0 -buth/2 0 +buth/2],'style','pop',...
         'string','Vector/Matrix Data|IDDATA object','callback',...
         'iduifile(''pop'');','fontweight','bold',...
         'tooltip',['Either data are column vectors/matrices or an ',...
            'IDDATA object.']);
      uicontrol(f,'pos',pos(2,:),'style','text','HorizontalAlignment',...
         'center','string','Data Format for Signals','Fontweight',...
         'bold');
      uicontrol(f,'pos',pos(1,:)+[FigW 0 0 0],'style','frame');
      uicontrol(f,'pos',pos(2,:)+[FigW 0 0 0],'style','text',...
         'string','Input Properties','horizontalalignment',...
         'center','fontweight','bold');
      XID.hload(1,15) = uicontrol(f,'pos',pos(3,:)+[FigW+butw 0 -butw 0],...
         'style','pop','string','zoh|foh|bl','tooltip',...
         ['zoh=Zero-Order-Hold, foh=First-Order-Hold, bl=BandLimited']);
      uicontrol(f,'pos',pos(3,:)+[FigW 0 -butw*1.2 0],'style','text',...
         'HorizontalAlignment','left','string','InterSample:');
      XID.hload(1,16) = uicontrol(f,'pos',pos(4,:)+...
         [FigW+butw 0 -butw*1 0],...
         'Backgroundcolor','w',...
         'HorizontalAlignment','left','style','Edit','string','inf',...
         'Tooltip',['Enter input period (same for all inputs).',...
            'Use ''inf'' for nonperiodic.']);
      uicontrol(f,'pos',pos(4,:)+[FigW 0 -butw*1.2 0],'style','text',...
         'HorizontalAlignment','left','string','Period:');
      FigWH=[FigW pos(1,2)+pos(1,4)+mEdgeToFrame];
      ScreenPos = get(0,'ScreenSize');
      Sumbpos=get(XID.sumb(1),'pos');
      FigXY=max([0,0],(Sumbpos(1:2)+Sumbpos(3:4)-[0,FigWH(2)]));
      FigXY=min(FigXY,ScreenPos(3:4)-FigWH);
      FigPos=[FigXY FigWH];%+[FigW 0]];
      %       l1 = uicontrol(f,'pos',[FigW 0 FigW FigWH(2)-2],...
      %          'style','frame');
      set(f,'pos',FigPos);
      set(get(f,'children'),'units','norm');
      if length(XID.layout)>18
         if XID.layout(19,3)
            eval('set(XID.hload(1,1),''pos'',XID.layout(19,1:4));','')
         end
      end  
      if nargin<3, set(XID.hload(1,1),'vis','on'),end
      iduistat('Enter input and output variable names.')
   end %if figflag
   set(Xsum,'UserData',XID);
   
elseif strcmp(arg,'load_model')
   cb1='iduipoin(1);iduistat(''Compiling...'');';
   cb2='iduipoin(1);';
   cb3='iduipoin(2);';
   
   FigName=idlaytab('figname',17);
   if ~figflag(FigName,0)
      iduistat('Opening the import dialog box ...')
      layout
      butw=mStdButtonWidth; buth=mStdButtonHeight;
      FigW=iduilay2(2);
      XID.hload(2,1)=figure('vis','off',...
         'NumberTitle','off','Name',FigName,...
         'Integerhandle','off','HandleVisibility','callback',...
         'Color',get(0,'DefaultUIControlBackgroundColor'),'tag','sitb17');
      set(XID.hload(2,1),'Menubar','none');
      strload=[];
      
      % LEVEL 1
      
      pos = iduilay1(FigW,4,2);
      uicontrol(XID.hload(2,1),'pos',pos(1,:),'style','frame');
      uicontrol(XID.hload(2,1),'pos',pos(2,:),...
         'style','push','string','Import','callback',...
         [cb1,'iduifile(''insert_model'');',cb3]);
      %  [cb1,'XIDarg=''insert_model'';iduifscr;',cb3]);
      uicontrol(XID.hload(2,1),'pos',pos(3,:),'style',...
         'push','string','Reset','callback','iduifile(''reset'',2)');
      uicontrol(XID.hload(2,1),'pos',pos(4,:),...
         'style','push','string','Close',...
         'callback','set(gcf,''vis'',''off'')');
      uicontrol(XID.hload(2,1),'pos',pos(5,:),...
         'style','push','string','Help',...
         'callback','iduihelp(''iduiimpm.hlp'',''Importing Models'');');
      
      % LEVEL 2 Frame for Data Info 
      lev2=pos(1,2)+pos(1,4);
      pos = iduilay1(FigW,5,5,lev2,4,4/2);
      uicontrol(XID.hload(2,1),'pos',pos(1,:),'style','frame');
      XID.hload(2,12) = uicontrol(XID.hload(2,1),'pos',pos(6,:)+...
         [0 0 0 2*buth+2*4],'style','edit',...
         'String','','max',2,'Backgroundcolor','w');
      uicontrol(XID.hload(2,1),'pos',pos(3,:),'style',...             
         'text','string','Notes:','Horizontalalignment','left');
      
      uicontrol(XID.hload(2,1),'pos',pos(2,:),...
         'style','text','string','Optional Model Information',...
         'HorizontalAlignment','center','Fontweight','bold');
      
      % LEVEL 3 Frame for Variable info
      lev3=pos(1,2)+pos(1,4);
      
      pos = iduilay1(FigW,5,5,lev3,3,2);
      uicontrol(XID.hload(2,1),'pos',pos(1,:),'style','frame');
      uicontrol(XID.hload(2,1),'pos',pos(2,:),'style','text','string',...
         'Model Variable','Fontweight','bold');
      uicontrol(XID.hload(2,1),'pos',pos(4,:)+[0 0 0 buth],'style','text','string',...
         'Enter the name of an IDMODEL workspace variable.', ...
         'HorizontalAlignment','left')
      
      XID.hload(2,4) = uicontrol(XID.hload(2,1),'pos',...
         pos(5,:),'style','edit','HorizontalAlignment','Left',...
         'String','','Backgroundcolor','w');%,'callback',...
      %[cb1,'XIDarg=''prep_model'';,iduifscr;',cb3]);
      FigWH=[FigW pos(1,2)+pos(1,4)+mEdgeToFrame];
      ScreenPos = get(0,'ScreenSize');
      Sumbpos=get(XID.sumb(1),'pos');
      FigXY=max([0,0],(Sumbpos(1:2)+Sumbpos(3:4)-[0,FigWH(2)]));
      FigXY=min(FigXY,ScreenPos(3:4)-FigWH);
      FigPos=[FigXY FigWH];
      set(XID.hload(2,1),'pos',FigPos);
      if length(XID.layout)>16
         if XID.layout(17,3)
            eval('set(XID.hload(2,1),''pos'',XID.layout(17,1:4));','')
         end
      end
      if nargin<3, set(XID.hload(2,1),'vis','on'),end
      set(get(XID.hload(2,1),'children'),'units','norm');
      iduistat('Enter model variable name.')
   end
   set(Xsum,'UserData',XID);
elseif strcmp(arg,'insert')
   iduistat('Inserting the data set ... ');
   
   dAta_n=get(XID.hload(1,4),'string');
   una = get(XID.hload(1,19),'string');
   yna = get(XID.hload(1,20),'string');
   uu = get(XID.hload(1,18),'string');
   yu = get(XID.hload(1,17),'string');
   inter = get(XID.hload(1,15),'value');
   per = get(XID.hload(1,16),'string');
   val = get(XID.hload(1,21),'Value');
   if val == 1
      inpn=get(XID.hload(1,11),'string');
      outn=get(XID.hload(1,13),'string');
      if isempty(outn)|strcmp(deblank(outn),'[]')
         errordlg('An output must always be supplied.','Error Dialog','modal')
         return
      end
      if isempty(dAta_n)
         dAta_n=[outn,inpn];set(XID.hload(1,4),'string',dAta_n),drawnow
      end
      if isempty(inpn)
         tsq = questdlg(str2mat('No input variable has been specified.',['Should a time',...
               ' series data set be created from the output variable?'],...
            '(To avoid this question in the future, enter ''[]'' for the input.)'),...
            'Time Series Data?');
         switch tsq
         case 'Yes'
            u = [];
         otherwise
            return
         end
      else
         try
            u = evalin('base',inpn);
         catch
            errordlg(['The input variable cannot be evaluated.',...
                  '\nCheck if the variable exists in the workspace and, in the ',...
                  'multi-input case, that the different inputs have the ',...
                  'same number of rows.'],'Error Dialog','modal'); 
            return
         end
      end
      try
         y = evalin('base',outn);
      catch
         errordlg(['The output variable cannot be evaluated.',...
               '\nCheck if the variable exists in the workspace and, in the ',...
               'multi-output case, that the different outputs have the ',...
               'same number of rows.'],'Error Dialog','modal'); 
         return
      end
      
      if isa(y,'iddata')|isa(u,'iddata')
         errordlg(['The output or input variable is an IDDATA object. ',...
               'Select the IDDATA object format as Data Format for Signals'],...
            'Error Dialog','modal');
         return
      end
      [Nny,ny]=size(y); % felmedd om 0
      if Nny==0
         iduistat('No data inserted.');
         return
      end
      
      [Nnu,nu]=size(u); % felmedd om 0
      if Nny~=Nnu&Nnu>0
         errordlg(['The input and output must have the same number',...
               ' of rows.'],'Error Dialog','modal');
         iduistat('No data inserted.');
         return
      end
      
      
      data = iddata(y,u);
   else % val ==2 iddata
      datn = get(XID.hload(1,11),'string');
      try
         data = evalin('base',datn); % Tests already made in iddata
         % callback
      catch
         errordlg(['The iddata variable cannot be evaluated.',...
               '\nCheck if the variable exists in the workspace.'],...
            'Error Dialog','modal'); 
         return
      end   
      if ~isa(data,'iddata')
         errordlg([datn,' is not an IDDATA object.']...
            ,'Error Dialog','modal');
         return
      end
   end
   
   Ne = length(pvget(data,'OutputData'));
   
   str1=get(XID.hload(1,6),'string');
   str2=get(XID.hload(1,8),'string');
   if iduifile('test1',str1,'tsamp');return,end
   if iduifile('test1',str2,'startt');return,end
   
   data=pvset(data,'Ts',eval(str1),'Tstart',eval(str2),'Name', ...
      dAta_n);
   [Ncap,ny,nu] =size(data);
   yna = deblank(yna);
   if ~isempty(yna)
      ycom = findstr(yna,',');
      ycom =[ycom, length(yna)+1];
      yname{1} = deblank(yna(1:ycom(1)-1));
      for k = 1:length(ycom)-1
         yname{k+1} = deblank(yna(ycom(k)+1:ycom(k+1)-1));
      end
      if length(yname)~=ny
         errordlg(...
            ['The number of OutputNames must equal the number of channels.',...
               ' The different names should be separed by commas in the edit field.'],'Error Dialog','modal');
         iduifile('more');
         return
      end
      data = pvset(data,'OutputName',yname);
      
   end
   una = deblank(una);
   if ~isempty(una)
      ucom = findstr(una,',');
      ucom =[ucom, length(una)+1];
      uname{1} = deblank(una(1:ucom(1)-1));
      for k = 1:length(ucom)-1
         uname{k+1} = deblank(una(ucom(k)+1:ucom(k+1)-1));
      end
      if length(uname)~=nu
         errordlg(['The number of InputNames must equal the number of channels.',...
               ' The different name should be separed by commas in the edit field.'],'Error Dialog','modal');
         iduifile('more');
         return
      end
      data = pvset(data,'InputName',uname);
   end
   yu = deblank(yu);
   if ~isempty(yu)
      ycom = findstr(yu,',');
      ycom =[ycom, length(yu)+1];
      yun{1} = deblank(yu(1:ycom(1)-1));
      for k = 1:length(ycom)-1
         yun{k+1} = deblank(yu(ycom(k)+1:ycom(k+1)-1));
      end
      if length(yun)~=ny
         errordlg(['The number of OutputUnits must equal the number of channels.',...
               ' The different name should be separed by commas in the edit field.'],'Error Dialog','modal');
         iduifile('more');
         return
      end
      data = pvset(data,'OutputUnit',yun);
      
   end
   uu = deblank(uu);
   if ~isempty(uu)
      ucom = findstr(uu,',');
      ucom =[ucom, length(uu)+1];
      uun{1} = deblank(uu(1:ucom(1)-1));
      for k = 1:length(ucom)-1
         uun{k+1} = deblank(uu(ucom(k)+1:ucom(k+1)-1));
      end
      if length(uun)~=nu
         errordlg(['The number of InputUnits must equal the number of channels.',...
               ' The different name should be separed by commas in the edit field.'],'Error Dialog','modal');
         iduifile('more');
         return
      end
      data = pvset(data,'InputUnit',uun);
   end
   
   SL=get(XID.hload(1,12),'string');
   if ~isempty(SL)
      data = iduiinfo('set',data,SL);%str2mat(dAta_info,SL);
   end
   data = iduiinfo('add',data,[' % Import   ',dAta_n]);
   if inter~=1
      for kexp = 1:Ne
         for ku = 1:nu
            if inter==2
               ints{ku,kexp} = 'foh';
            elseif inter==3
               ints{ku,kexp} = 'bl';
            end
         end
      end
      data = pvset(data,'InterSample',ints);
   end
   try
      per = eval(per);
   catch
      per = [];
   end
   
   if isempty(per)
      for kexp =1:Ne
         per{kexp} = inf;
      end
   end
   if ~iscell(per)
      per = {per};
   end
   err = 0;
   for kexp = 1:Ne
      pers = per{kexp};
      if ~strcmp(pers,'inf')
         
         if ~isa(pers,'double')
            err=1;
         elseif length(pers)>1
            err=1;
         elseif pers<=0|floor(pers)~=pers
            err = 1;
         end
      end
      perss{kexp} = ones(nu,1)*pers;
   end
   if err
      errordlg(['The period must either be given as inf (no quotes)',...
            ' or as a positive integer. The same period',...
            ' is assumed for all input channels.',...
            '\nFor multiexperiment data use a' ...
            ' cell array.'],'Error Dialog','modal');
      return
   end
   try
      data = pvset(data,'Period',perss);
   catch
      errordlg(lasterr,'Error Dialog','modal')
   end
   
   
   
   iduiinsd(data,1);%,dAta_info,dAta_n);
elseif strcmp(arg,'iddata')
   
   if get(XID.hload(1,21),'Value') == 2
      iddn = get(XID.hload(1,11),'string');
      try
         idd = evalin('base',iddn);
      catch
         errordlg(['The iddata variable cannot be evaluated.',...
               '\nCheck if the variable exists in the workspace.'],'Error Dialog','modal'); 
         return
      end   
      if ~isa(idd,'iddata')
         errordlg([iddn,' is not an IDDATA object.'],'Error Dialog','modal');
         return
      end
      data_n = pvget(idd,'Name');
      if isempty(data_n)
         data_n = iddn;
      end
      set(XID.hload(1,4),'string',data_n);
      yna = pvget(idd,'OutputName');
      ynna = [];
      for ky = 1:length(yna)
         ynna = [ynna,yna{ky},','];
      end
      ynna = ynna(1:end-1);
      una = pvget(idd,'InputName');
      unna = [];
      for ky = 1:length(una)
         unna = [unna,una{ky},','];
      end
      unna = unna(1:end-1);
      uu = pvget(idd,'InputUnit');
      unu = [];
      for ky = 1:length(uu)
         unu = [unu,uu{ky},','];
      end
      unu = unu(1:end-1);
      yu = pvget(idd,'OutputUnit');
      ynu = [];
      for ky = 1:length(yu)
         ynu = [ynu,yu{ky},','];
      end
      ynu = ynu(1:end-1);
      Ts = pvget(idd,'Ts'); 
      Tstart = pvget(idd,'Tstart'); 
      per = pvget(idd,'Period');
      Tsstr = num2str(Ts{1});
      Tssatr = num2str(Tstart{1});
      pers = per{1};
      perst = int2str(pers(1)); %Same per for all inputs
      for kexp = 2:length(Ts)
         Tsstr=[Tsstr,',',num2str(Ts{kexp})];
         Tssatr=[Tssatr,',',num2str(Tstart{kexp})];
         pers = per{kexp};
         perst=[perst,',',int2str(pers(1))];
      end
      if length(Ts)>1
         Tsstr=['{',Tsstr,'}'];
         Tssatr=['{',Tssatr,'}'];
         perst=['{',perst,'}'];
      end
      
      int = pvget(idd,'InterSample');int = int{1,1};
      set(XID.hload(1,6),'string',Tsstr);
      set(XID.hload(1,8),'string',Tssatr);
      set(XID.hload(1,19),'string',unna);
      set(XID.hload(1,20),'string',ynna);
      set(XID.hload(1,18),'string',unu);
      set(XID.hload(1,17),'string',ynu);
      switch int
      case 'zoh'
         popv = 1;
      case 'foh'
         popv = 2;
      case 'bl'
         popv = 3;
      end
      set(XID.hload(1,15),'value',popv);
      set(XID.hload(1,16),'string',perst);
      try
         set(XID.hload(1,12),'string',pvget(idd,'Notes'));
      catch
      end
   end
elseif strcmp(arg,'reset')
   iduistat('Resetting the dialog entries ...')   
   if file==2
      set(XID.hload(2,[4,12]),'string','');
   elseif any(file==[1,3])
      set(XID.hload(file,[4,12]),'string','');
      try
         set(XID.hload(file,8),'string',int2str(1));
         set(XID.hload(file,6),'string',int2str(1));
      catch
      end
      if file==1,
         set(XID.hload(1,[11,13,17:20]),'string','');
         set(XID.hload(1,16),'string','inf');
         set(XID.hload(1,15),'value',1);
      end
      % if file==3,set(XID.hload(3,10),'string',int2str(1));end
   end
   iduistat('')
elseif strcmp(arg,'test1')
   % Test if sampling interval is reasonable
   if nargin<2
      Tsstring=get(iduigco,'string');
      tagg=get(iduigco,'tag');
   else
      Tsstring=file;
      tagg=dum;
   end
   outarg=0;
   try
      testts=eval(Tsstring);
   catch
      outarg=1;
   end
   if strcmp(tagg,'tsamp')
      msg=['The sampling interval could not be evaluated.',...
            '\nIt must be given as a positive real number.',...
            '\nVariable names in the workspace cannot be used.',...
            '\nFor Multiexperiment data use a cell array.'];
   else
      msg=['The starting time could not be evaluated.',...
            '\nIt must be given as a real number.',...
            '\nVariable names in the workspace cannot be used.',...
            '\nFor Multiexperiment data use a cell array.'];
   end
   if outarg
      errordlg(msg,'Error Dialog','modal');
      return
   end
   if ~iscell(testts)
      testts1={testts};
   else
      testts1 = testts;
   end
   for kexp = 1:length(testts1)
      testts=testts1{kexp};
      
      if length(testts)>1|length(testts)<1,
         outarg = 1;
      end
      
      if isstr(testts),outarg=1;end
      if testts(1)<=0&strcmp(tagg,'tsamp'),outarg=1;end
   end
   if outarg
      errordlg(msg,'Error Dialog','modal');
      return
   end
elseif strcmp(arg,'more')
   if strcmp(get(XID.hload(1,14),'String'),'Less')
      return
   end
   
   f = XID.hload(1,1);
   set(f,'unit','pixel');
   set(get(XID.hload(1,1),'children'),'units','pixel');
   
   pos = get(f,'pos');
   set(f,'pos',[pos(1:2),2*pos(3),pos(4)]);
   set(f,'unit','norm');
   set(get(XID.hload(1,1),'children'),'units','norm');
   
   set(XID.hload(1,14),'String','Less','callback','iduifile(''less'')');
elseif strcmp(arg,'less')
   f = XID.hload(1,1);
   set(f,'unit','pixel');
   set(get(XID.hload(1,1),'children'),'units','pixel');
   
   pos = get(f,'pos');
   set(f,'pos',[pos(1:2),pos(3)/2,pos(4)]);
   set(f,'unit','norm');
   set(get(XID.hload(1,1),'children'),'units','norm');
   
   set(XID.hload(1,14),'String','More','callback', ...
      'iduifile(''more'')');
elseif strcmp(arg,'pop')
   switch get(XID.hload(1,21),'Value')
   case 1
      %set(XID.hload(1,22),'String','Input Output Vectors/Matrices');
      set(XID.hload(1,24),'String','Input:')
      set(XID.hload(1,11),'tooltip',...
         ['Any MATLAB expression that evaluates to an N-by-Nu matrix']);
      set(XID.hload(1,[13,25]),'enable','on');
      
   case 2
      %set(XID.hload(1,22),'String','IDDATA Object');
      set(XID.hload(1,24),'String','Iddata:');
      set(XID.hload(1,11),'tooltip',...
         ['Enter the name of an IDDATA object. See Help Iddata.']);
      set(XID.hload(1,[13,25]),'enable','off');
      set(XID.hload(1,13),'String','');
   end
elseif strcmp(arg,'insert_model')
   name = get(XID.hload(2,4),'string');
   if isempty(name)
      errordlg('A model name must be supplied.','Error Dialog','modal')
      return
   end
   try
      mod = evalin('base',name); 
   catch
      errordlg(['The variable ',name,' does not exist in workspace.'],'Error Dialog','modal')
      return
   end
   if ~isa(mod,'idmodel') 
      if isa(mod,'lti')
         mod = idss(mod);
      elseif isa(mod,'idfrd')
      else
         try
            mod = th2ido(mod);
         catch
            errordlg(['The variable ',name,' is not of a recognized model type.'],'Error Dialog','modal')
            return
         end
      end        
   end
   try
      if isempty(pvget(mod,'Name'))
         nr = findstr(name,'{');
         nr1 = findstr(name,'('); 
         nr = min([nr,nr1]);
         if ~isempty(nr)
            if nr == 1
               errordlg('Please remove parentheses in model name.','Error Dialog','modal')
               return
            end
            name = name(1:min(nr)-1);
         end
         mod = pvset(mod,'Name',name);
      end
   catch
   end
   info = get(XID.hload(2,12),'string');
   if ~isempty(info);
      mod = iduiinfo('set',mod,info);
   end
   iduiinsm(mod,0,[],1);
   
end
