%SCRIPT FILE IDUIFSCR 
%       This script file is a complement to the function IDUIFILE
%       for handling import of data and models
%       The switchboard variable is called XIDarg. It takes the following values:
%       prep_data    Sets up certain variables to prepare for 'insert_data'
%       prep_model   Ditto for models
%       insert_data  Inserts the data into the summary board
%       insert_model Inserts the model into the summary board

%	L. Ljung 4-4-94
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.7 $  $Date: 1995/05/02 16:32:08 $

global XIDhload
if strcmp(XIDarg,'prep_dataio')
   XIDinput=get(XIDhload(1,11),'string');
   XIDoutput=get(XIDhload(1,13),'string');
   if ~isempty(XIDinput)|~isempty(XIDoutput)
      XIDstopp=0;
      eval('XIDXy=eval([''['',XIDoutput,'']'']);','XIDstopp=1;');
      if XIDstopp
	 errordlg(str2mat('The output variable cannot be evaluated.',...
	 'Check if the variable exists in the workspace and, in the',...
	 'multi-output case, that the different outputs have the',...
	 'same number of rows.'));
	 set(XIDhload(1,13),'userdata',[]);XIDarg='clear';iduifscr;return 
      end
      [XIDNny,XIDny]=size(XIDXy);if XIDNny<XIDny,XIDXy=XIDXy';end
      set(XIDhload(1,13),'userdata',XIDXy);
      eval('XIDXu=eval([''['',XIDinput,'']'']);','XIDstopp=1;');
      if XIDstopp
	 errordlg(str2mat('The input variable cannot be evaluated.',...
	 'Check if the variable exists in the workspace and, in the',...
	 'multi-input case, that the different inputs have the',...
	 'same number of rows.'));
         set(XIDhload(1,11),'userdata',[]);set(XIDhload(1,13),'userdata',[]);
         XIDarg='clear';iduifscr;return 
      end
      [XIDNnu,XIDnu]=size(XIDXu);if XIDNnu<XIDnu,XIDXu=XIDXu';end
      set(XIDhload(1,11),'userdata',XIDXu);
   end % if notempty ..

elseif strcmp(XIDarg,'prep_data')
   XIDdAta_n=get(XIDhload(3,4),'string');
   if ~isempty(XIDdAta_n)
       XIDdAta_n=XIDdAta_n(1,:);XIDdAta_n=XIDdAta_n(find(XIDdAta_n~=' '));
       XIDDAinf=[XIDdAta_n,'_info'];
       if exist(XIDDAinf),XIDDAinf=eval(XIDDAinf);
           [XIDSL1,XIDSL2]=size(XIDDAinf);
           set(XIDhload(3,6),'string',XIDDAinf(1,:));
           set(XIDhload(3,10),'value',eval(XIDDAinf(2,:)));
           set(XIDhload(3,8),'string',XIDDAinf(3,:));
           if XIDSL1>7,set(XIDhload(3,12),'string',XIDDAinf(8:XIDSL1,:));end
       end
   else
      errordlg('Data name must be supplied.');
   end
elseif strcmp(XIDarg,'insert_data')
   XIDstrload=[];
   iduistat('Inserting the data set ...')

   XIDdAta_n=get(XIDhload(3,4),'string');
   if isempty(XIDdAta_n)
      errordlg('Data name must be supplied.');
      XIDarg='clear';iduifscr;return
   end
   XIDdAta_n=XIDdAta_n(1,:);
   XIDdAta_n=XIDdAta_n(find(XIDdAta_n~=' '));
   if exist(XIDdAta_n), 
       [XIDN,XIDnz]=size(eval(XIDdAta_n));
       XIDnustr=[];XIDnystr=[];
       err=0;
       eval('XIDny=eval(get(XIDhload(3,10),''string''));','err=1;')
       msg=str2mat('The entry for number of outputs is not acceptable.',...
              'It must be a positive integer, less or equal to the number',...
              'of columns in the data matrix.');
       if err,errordlg(msg);XIDarg='clear';iduifscr;return,end
       if length(XIDny)>1|length(XIDny)<1,errordlg(msg);XIDarg='clear';iduifscr;return,end
       if floor(XIDny)~=XIDny,errordlg(msg);XIDarg='clear';iduifscr;return,end
       if XIDny<1|XIDny>XIDnz,errordlg(msg);XIDarg='clear';iduifscr;return,end

       XIDnu=XIDnz-XIDny; 
       for XIDkk=1:XIDnu
         XIDnustr=[XIDnustr,int2str(XIDkk),' '];
       end
       for XIDkk=1:XIDny
         XIDnystr=[XIDnystr,int2str(XIDkk),' '];
       end
       str1=get(XIDhload(3,6),'string');
       str2=get(XIDhload(3,8),'string');
       if iduifile('test1',str1,'tsamp'),XIDarg='clear';iduifscr;return,end
       if iduifile('test1',str2,'startt'),XIDarg='clear';iduifscr;return,end
       XIDdAta_info=str2mat(str1,int2str(XIDny),str2,...
			int2str(XIDN),int2str(XIDnu),XIDnystr,XIDnustr);
      XIDSL=get(XIDhload(3,12),'string');
      if ~isempty(XIDSL)
	 XIDdAta_info=str2mat(XIDdAta_info,XIDSL);
      end
      XIDdAta_info=str2mat(XIDdAta_info,...
	XIDstrload,[' Import   ',XIDdAta_n]);
      iduiinsd(eval(XIDdAta_n),XIDdAta_info,XIDdAta_n);
   else
      errordlg('The variable does not exist in the workspace.');
      XIDarg='clear';iduifscr;return
   end
elseif strcmp(XIDarg,'prep_model') 
   XIDdAta_n=get(XIDhload(2,4),'string');
   if isempty(XIDdAta_n)
      errordlg('Model name must be supplied.');
      XIDarg='clear';iduifscr;return
   end
   XIDdAta_n=XIDdAta_n(1,:);XIDdAta_n=XIDdAta_n(find(XIDdAta_n~=' '));
   XIDDAinf=[XIDdAta_n,'_info'];
   if exist(XIDDAinf),
      XIDDAinf=eval(XIDDAinf);
   else
      XIDDAinf=str2mat('imported','','','','','','','');
   end
   [XIDSL1,XIDSL2]=size(XIDDAinf);
   set(XIDhload(2,12),'UserData',XIDDAinf(1:8,:))
   if XIDSL1>8,set(XIDhload(2,12),'string',XIDDAinf(9:XIDSL1,:));end


elseif strcmp(XIDarg,'insert_model')
   iduistat('Inserting the model ...')
   XIDstrload=[];
   XIDdAta_n=get(XIDhload(2,4),'string');
   if isempty(XIDdAta_n)
      errordlg('Model name must be supplied.');
      XIDarg='clear';iduifscr;return
   end
   XIDdAta_n=XIDdAta_n(1,:);
   XIDEQual=find(XIDdAta_n=='=');
   if ~isempty(XIDEQual)
      eval(XIDdAta_n)
      XIDstrload=str2mat(XIDstrload,XIDdAta_n);
      XIDdAta_n=deblank(XIDdAta_n(1:XIDEQual-1));
   end
   XIDdAta_n=XIDdAta_n(1,find(XIDdAta_n(1,:)~=' '));
   if exist(XIDdAta_n), 
      XIDdAta_info=get(XIDhload(2,12),'UserData');
      XIDSL=get(XIDhload(2,12),'string');
      if ~isempty(XIDSL)
         XIDdAta_info=str2mat(XIDdAta_info,XIDSL);
      end
      XIDdAta_info=str2mat(XIDdAta_info,...
        XIDstrload,['Import   ',XIDdAta_n]);
      iduiinsm(eval(XIDdAta_n),XIDdAta_info,XIDdAta_n,0);
   else
      errordlg('The variable name does not exist in the workspace.');
   end
   iduistat('')
end

clear XIDarg  XIDinput XIDoutput XIDstopp
clear XIDNny XIDny XIDNnu XIDnu XIDXu XIDXy XIDdAta_n XIDDAint 
clear XIDSL1 XIDSL2
clear XIDdats XIDdatn XIDmodn XIDkc XIDlnamn XIDstrload XIDloadcom XIDN XIDnz
clear XIDkk XIDnustr XIDnystr XIDdAta_info XIDSL XIDEQual XIDDAinf
