function outarg=iduifile(arg,file,dum)
%IDUIFILE Handles file operations in ident.
%	Arguments:
%	load        Opens MATLAB's file finder
%	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
%	find_vars   Searches the indicated .mat file for its variables
%	load_mat    Loads the selected mat file
%	done_data   Final operations when inserting data
%	reset       Resets the dialog's different boxes
%       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 (c) 1986-95 by the MathWorks, Inc.
%       $Revision: 1.12 $  $Date: 1995/05/10 21:36:59 $
	
global XIDhdat XIDhload XIDsumb XIDlayout

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(4);
       XIDhload(1,1)=figure('vis','off',...
	     'NumberTitle','off','Name',FigName,...
	     'Color',get(0,'DefaultUIControlBackgroundColor'),'tag','sitb19');
      set(XIDhload(1,1),'Menubar','none');
      strload=[];

      % LEVEL 1

       pos = iduilay1(FigW,4);
       uicontrol(XIDhload(1,1),'pos',pos(1,:),'style','frame');
       uicontrol(XIDhload(1,1),'pos',pos(2,:),...
	    'style','push','string','Import','callback',...
	    [cb1,'eval(''iduifile(''''insert'''');''),',cb3]);
      uicontrol(XIDhload(1,1),'pos',pos(3,:),'style',...
              'push','string','Reset','callback','iduifile(''reset'',1)');
      uicontrol(XIDhload(1,1),'pos',pos(4,:),...
             'style','push','string','Close',...
	     'callback','set(gcf,''vis'',''off'')');
      uicontrol(XIDhload(1,1),'pos',pos(5,:),...
             'style','push','string','Help',...
	     'callback','iduihelp(''iduiimp2.hlp'',''Importing Data'');');
     
      % LEVEL 2 Frame for Data Info 
      lev2=pos(1,2)+pos(1,4);
      pos = iduilay1(FigW,18,9,lev2,4,[1.5 2.5]);
      uicontrol(XIDhload(1,1),'pos',pos(1,:),'style','frame');
      XIDhload(1,12) = uicontrol(XIDhload(1,1),'pos',...
             [pos(18,1:2) FigW-2*pos(18,1) 4*buth+3*4],...
	      'style','edit',...
               'HorizontalAlignment','left',...
              'String','','max',2,'Backgroundcolor','w');
      uicontrol(XIDhload(1,1),'pos',...
              pos(10,:),'style','text',...
               'HorizontalAlignment','left',...
	      'string','Notes:');
     uicontrol(XIDhload(1,1),'pos',pos(4,:),'style',...
              'text','string','Data name:',...
               'HorizontalAlignment','left');
     XIDhload(1,4) = uicontrol(XIDhload(1,1),'pos',pos(5,:),'style','edit',...
               'HorizontalAlignment','left',...
              'String','mydata','Backgroundcolor','w');
      uicontrol(XIDhload(1,1),'pos',pos(6,:),...
              'String','Starting time:','style','text',...
              'horizontalalignment','left');
      XIDhload(1,8) = uicontrol(XIDhload(1,1),'pos',pos(7,:),...
               'style','edit','String','1','tag','startt',...
            'HorizontalAlignment','left','callback','iduifile(''test1'');',...
              'Backgroundcolor','w');
      uicontrol(XIDhload(1,1),'pos',pos(8,:),...
              'String','Sampling interval:','style','text',...
              'horizontalalignment','left');
      XIDhload(1,6) = uicontrol(XIDhload(1,1),'pos',pos(9,:),'tag','tsamp',...
           'HorizontalAlignment','left','callback','iduifile(''test1'');',...
              'style','edit','String','1','Backgroundcolor','w');
      uicontrol(XIDhload(1,1),'pos',[pos(2,1:2) ...
               FigW-2*(mEdgeToFrame+mFrameToText) pos(2,4)],...
              'style','text','string','Optional Data Information',...
              'HorizontalAlignment','center');
  
     % LEVEL 3 Frame for Variable info

     lev3=pos(1,2)+pos(1,4);

    pos = iduilay1(FigW,8,4,lev3,4,[1.5 2.5]);
     uicontrol(XIDhload(1,1),'pos',pos(1,:),'style','frame');
     uicontrol(XIDhload(1,1),'pos',pos(6,:),...
               'HorizontalAlignment','left',...
              'style','text','string','Input:')
     XIDhload(1,11)=...
            uicontrol(XIDhload(1,1),'pos',pos(7,:),...
              'style','edit','Backgroundcolor','w',...
               'HorizontalAlignment','left',...
              'callback',[cb2,'XIDarg=''prep_dataio'';,iduifscr;',cb3]);
     uicontrol(XIDhload(1,1),'pos',pos(8,:),...
              'style','text','string','Output:',...
               'HorizontalAlignment','left');
     XIDhload(1,13)=...
            uicontrol(XIDhload(1,1),'pos',pos(9,:),...
              'style','edit','Backgroundcolor','w',...
               'HorizontalAlignment','left',...
              'callback',[cb2,'XIDarg=''prep_dataio'';,iduifscr;',cb3]);
     uicontrol(XIDhload(1,1),'pos',pos(4,:)+[0 0 2*butw 0],'style','text',...
      'HorizontalAlignment','left',...
      'string',...
      'Enter workspace variable names.');
     uicontrol(XIDhload(1,1),'pos',...
        [pos(2,1:2) FigW-2*(mEdgeToFrame+mFrameToText) pos(2,4)],...
         'style','text','string',...
        'Input Output Variables');
     FigWH=[FigW pos(1,2)+pos(1,4)+mEdgeToFrame];
       ScreenPos = get(0,'ScreenSize');
       FigPos=[max((ScreenPos(3:4)-FigWH)/2,[0 0]) FigWH];
    set(XIDhload(1,1),'pos',FigPos);
    set(get(XIDhload(1,1),'children'),'units','norm');
    if length(XIDlayout)>18
      if XIDlayout(19,3)
        eval('set(XIDhload(1,1),''pos'',XIDlayout(19,1:4));','')
      end
    end  
    if nargin<3, set(XIDhload(1,1),'vis','on'),end
    iduistat('Enter input and output variable names.')
    end %if figflag
elseif strcmp(arg,'load_data')
  cb1='iduipoin(1);iduistat(''Compiling...'');';
  cb2='iduipoin(1);';
  cb3='iduipoin(2);';

  FigName=idlaytab('figname',18);
  if ~figflag(FigName,0)
       iduistat('Opening the import dialog box ...')
       layout
       butw=mStdButtonWidth; buth=mStdButtonHeight;
       FigW = iduilay2(4);
       XIDhload(3,1)=figure('vis','off',...
	     'NumberTitle','off','Name',FigName,...
	     'Color',get(0,'DefaultUIControlBackgroundColor'),'tag','sitb18');
      set(XIDhload(3,1),'Menubar','none');
      strload=[];

      % LEVEL 1

       pos = iduilay1(FigW,4);
       uicontrol(XIDhload(3,1),'pos',pos(1,:),'style','frame');
       uicontrol(XIDhload(3,1),'pos',pos(2,:),...
	    'style','push','string','Import','callback',...
	    [cb1,'XIDarg=''insert_data'';eval(''iduifscr;''),',cb3]);
      uicontrol(XIDhload(3,1),'pos',pos(3,:),'style',...
              'push','string','Reset','callback','iduifile(''reset'',3)');
      uicontrol(XIDhload(3,1),'pos',pos(4,:),...
             'style','push','string','Close',...
	     'callback','set(gcf,''vis'',''off'')');
      uicontrol(XIDhload(3,1),'pos',pos(5,:),...
             'style','push','string','Help',...
	     'callback','iduihelp(''iduiimp1.hlp'',''Importing Data'');');
     
      % LEVEL 2 Frame for Data Info
      lev2=pos(1,2)+pos(1,4);
      pos = iduilay1(FigW,18,9,lev2,4,[1.5 2.5]);
      uicontrol(XIDhload(3,1),'pos',pos(1,:),'style','frame');
      XIDhload(3,12) = uicontrol(XIDhload(3,1),'pos',...
             [pos(18,1:2) FigW-2*pos(18,1) 4*buth+3*4],...
	      'style','edit',...
               'HorizontalAlignment','left',...
              'String','','max',2,'Backgroundcolor','w');
      uicontrol(XIDhload(3,1),'pos',...
              pos(10,:),'style','text',...
               'HorizontalAlignment','left',...
	      'string','Notes:');
     uicontrol(XIDhload(3,1),'pos',pos(4,:),'style',...
              'text','string','Number of outputs:',...
               'HorizontalAlignment','left');
     XIDhload(3,10) = uicontrol(XIDhload(3,1),'pos',pos(5,:),'style','edit',...
               'HorizontalAlignment','left',...
              'String','1','Backgroundcolor','w');
      uicontrol(XIDhload(3,1),'pos',pos(6,:),...
              'String','Starting time:','style','text',...
              'horizontalalignment','left');
      XIDhload(3,8) = uicontrol(XIDhload(3,1),'pos',pos(7,:),...
               'style','edit','String','1','tag','startt',...
            'HorizontalAlignment','left','callback','iduifile(''test1'');',...
              'Backgroundcolor','w');
      uicontrol(XIDhload(3,1),'pos',pos(8,:),...
              'String','Sampling interval:','style','text',...
              'horizontalalignment','left');
      XIDhload(3,6) = uicontrol(XIDhload(3,1),'pos',pos(9,:),'tag','tsamp',...
            'HorizontalAlignment','left','callback','iduifile(''test1'');',...
              'style','edit','String','1','Backgroundcolor','w');
      uicontrol(XIDhload(3,1),'pos',[pos(2,1:2) ...
               FigW-2*(mEdgeToFrame+mFrameToText) pos(2,4)],...
              'style','text','string','Optional Data Information',...
              'HorizontalAlignment','center');
   
     % LEVEL 3 Frame for Variable info
     lev3=pos(1,2)+pos(1,4);
     pos = iduilay1(FigW,5,5,lev3,4,4);
     uicontrol(XIDhload(3,1),'pos',pos(1,:),'style','frame');
     uicontrol(XIDhload(3,1),'pos',pos(2,:),'style','text','string',...
         'Data Variable');
     uicontrol(XIDhload(3,1),'pos',pos(3,:),'style','text',...
          'HorizontalAlignment','left',...
          'String','Enter below the name of a workspace variable. Its');
     uicontrol(XIDhload(3,1),'pos',pos(4,:),'style','text',...
          'HorizontalAlignment','left',...
          'String','first column(s) should contain the data outputs and');
     uicontrol(XIDhload(3,1),'pos',pos(5,:),'style','text',...
          'HorizontalAlignment','left',...
          'String','the next column(s) should contain the data inputs')

     XIDhload(3,4) = uicontrol(XIDhload(3,1),'pos',...
              pos(6,:),'style','edit',...
              'HorizontalAlignment','left',...
              'String','','Backgroundcolor','w','callback',...
              [cb1,'XIDarg=''prep_data'';,iduifscr;',cb3]);
    FigWH=[FigW pos(1,2)+pos(1,4)+mEdgeToFrame];
    ScreenPos = get(0,'ScreenSize');
    FigPos=[max((ScreenPos(3:4)-FigWH)/2.5,[0 0]) FigWH];
    set(XIDhload(3,1),'pos',FigPos);
    if length(XIDlayout)>18
      if XIDlayout(18,3)
        eval('set(XIDhload(3,1),''pos'',XIDlayout(18,1:4));','')
      end
    end
     if nargin<3, set(XIDhload(3,1),'vis','on'),end
    set(get(XIDhload(3,1),'children'),'units','norm');
    iduistat('Enter data variable name.')
    end

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(4);
       XIDhload(2,1)=figure('vis','off',...
	     'NumberTitle','off','Name',FigName,...
	     'Color',get(0,'DefaultUIControlBackgroundColor'),'tag','sitb17');
      set(XIDhload(2,1),'Menubar','none');
      strload=[];

      % LEVEL 1

       pos = iduilay1(FigW,4);
       uicontrol(XIDhload(2,1),'pos',pos(1,:),'style','frame');
       uicontrol(XIDhload(2,1),'pos',pos(2,:),...
	    'style','push','string','Import','callback',...
	    [cb1,'XIDarg=''insert_model'';iduifscr;',cb3]);
      uicontrol(XIDhload(2,1),'pos',pos(3,:),'style',...
              'push','string','Reset','callback','iduifile(''reset'',2)');
      uicontrol(XIDhload(2,1),'pos',pos(4,:),...
             'style','push','string','Close',...
	     'callback','set(gcf,''vis'',''off'')');
      uicontrol(XIDhload(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,7,7,lev2,4,4);
      uicontrol(XIDhload(2,1),'pos',pos(1,:),'style','frame');
      XIDhload(2,12) = uicontrol(XIDhload(2,1),'pos',pos(8,:)+...
              [0 0 0 4*buth+4*4],'style','edit',...
              'String','','max',2,'Backgroundcolor','w');
      uicontrol(XIDhload(2,1),'pos',pos(3,:),'style',...             
	      'text','string','Notes:','Horizontalalignment','left');

      uicontrol(XIDhload(2,1),'pos',pos(2,:),...
              'style','text','string','Optional Model Information',...
              'HorizontalAlignment','center');
  
     % LEVEL 3 Frame for Variable info
      lev3=pos(1,2)+pos(1,4);

     pos = iduilay1(FigW,4,4,lev3,3,3);
     uicontrol(XIDhload(2,1),'pos',pos(1,:),'style','frame');
     uicontrol(XIDhload(2,1),'pos',pos(2,:),'style','text','string',...
         'Model Variable');
     uicontrol(XIDhload(2,1),'pos',pos(3,:),'style','text','string',...
          'Enter the name of a workspace variable in ''theta format''', ...
          'HorizontalAlignment','left')

     XIDhload(2,4) = uicontrol(XIDhload(2,1),'pos',...
              pos(4,:),'style','edit',...
              'String','','Backgroundcolor','w','callback',...
              [cb1,'XIDarg=''prep_model'';,iduifscr;',cb3]);
    FigWH=[FigW pos(1,2)+pos(1,4)+mEdgeToFrame];
       ScreenPos = get(0,'ScreenSize');
       FigPos=[max((ScreenPos(3:4)-FigWH)/3,[0 0]) FigWH];
    set(XIDhload(2,1),'pos',FigPos);
    if length(XIDlayout)>16
      if XIDlayout(17,3)
        eval('set(XIDhload(2,1),''pos'',XIDlayout(17,1:4));','')
      end
    end
     if nargin<3, set(XIDhload(2,1),'vis','on'),end
    set(get(XIDhload(2,1),'children'),'units','norm');
    iduistat('Enter model variable name.')
    end
elseif strcmp(arg,'insert')

   iduistat('Inserting the data set ...')
    strload=[];
   input=get(XIDhload(1,11),'userdata');
   output=get(XIDhload(1,13),'userdata');
   inpn=get(XIDhload(1,11),'string');
   outn=get(XIDhload(1,13),'string');
   dAta_n=get(XIDhload(1,4),'string');
   if isempty(dAta_n)
      dAta_n=[outn,inpn];set(XIDhload(1,4),'string',dAta_n),drawnow
   end
      [Nny,ny]=size(output); % felmedd om 0
      if Nny==0
         errordlg(str2mat('The output variable is not defined.',...
                          'If you have just loaded the data into workspace,',...
                          'retype the output variable name in the edit box.'));
      return
      end

      [Nnu,nu]=size(input); % felmedd om 0
      if Nny~=Nnu&Nnu>0
         errordlg('The input and output must have the same number of rows.');
      return
      end
      if isempty(inpn)
         loadcom=[' ',dAta_n,' = [',outn,'];'];
      else
         loadcom=[' ',dAta_n,'=[[',outn,'],[ ',inpn,']];'];
      end
      strload=str2mat(strload,loadcom);
       nustr=[];nystr=[];
       for kk=1:nu
         nustr=[nustr,int2str(kk),' '];
       end
       for kk=1:ny
         nystr=[nystr,int2str(kk),' '];
       end
       str1=get(XIDhload(1,6),'string');
       str2=get(XIDhload(1,8),'string');
       if iduifile('test1',str1,'tsamp');return,end
       if iduifile('test1',str2,'startt');return,end
       dAta_info=str2mat(str1,int2str(ny),str2,...
			int2str(Nny),int2str(nu),nystr,nustr);
      SL=get(XIDhload(1,12),'string');
      if ~isempty(SL)
	 dAta_info=str2mat(dAta_info,SL);
      end
      dAta_info=str2mat(dAta_info,...
	strload,[' % Import   ',dAta_n]);
      iduiinsd([output input],dAta_info,dAta_n);

elseif strcmp(arg,'reset')
 iduistat('Resetting the dialog entries ...')   
 if file==2
   set(XIDhload(2,[4,12]),'string','');
 elseif any(file==[1,3])
   set(XIDhload(file,[4,12]),'string','');
   set(XIDhload(file,8),'string',int2str(1));
   set(XIDhload(file,6),'string',int2str(1));
   if file==1,set(XIDhload(1,[11,13]),'string','');end
   if file==3,set(XIDhload(3,10),'string',int2str(1));end
  end
 iduistat('')
elseif strcmp(arg,'test1')
       % Test if sampling interval is reasonable
    if nargin<2
       Tsstring=get(gco,'string');
       tagg=get(gco,'tag');
    else
       Tsstring=file;
       tagg=dum;
    end
       outarg=0;
       eval('testts=eval(Tsstring);','outarg=1;')
       if strcmp(tagg,'tsamp')
          msg=str2mat('The sampling interval could not be evaluated.',...
             'It must be given as a positive real number.',...
             'Variable names in the workspace cannot be used.');
       else
          msg=str2mat('The starting time could not be evaluated.',...
             'It must be given as a real number.',...
             'Variable names in the workspace cannot be used.');
       end
       if outarg
          errordlg(msg);
          return
       end
       if length(testts)>1|length(testts)<1,errordlg(msg);return,end
       if isstr(testts),outarg=1;end
       if testts(1)<=0&strcmp(tagg,'tsamp'),outarg=1;end
       if outarg
          errordlg(msg);
          return
       end
end
