function ReturnValue=ImportXL(fn,arg)

%ImportXL	This function is a GUI that automates the importing of 
%		data from Microsoft Excel into a standard Matlab matrix.
%
%SYNTAX:	MatrixName = ImportXL
%
%		This function returns the selected data from Excel
%		and places it in the variable MatrixName.  The link
%		is retained to Excel, so modifying the data in the
%		Excel spreadsheet will modify the matrix data.

%    Copyright (C) 1996  Brad North

%    This program is free software; you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation; either version 2 of the License, or
%    (at your option) any later version.

%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.

%    You should have received a copy of the GNU General Public License
%    along with this program; if not, write to the Free Software
%    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

%    Problems, Comments, and Suggestions can be sent to 
%            northbh@nextwork.rose-hulman.edu

if nargin == 0
	fn = 'init';
end;

global c_link ExcelMatrix VariableName;

Figure_Name='Import Excel Data';

%Activates the figure and makes sure it has been properly initialized

if ~strcmp(fn,'init')
	h_MainFig = gcf;

%If the current figure is not named 'Figure_Name then find 
%the figure that is and put its handle in h_MainFig. This will 
%be found by checking the children of root.

%This is what the name of the figure will be.

	if ~strcmp(get(h_MainFig,'name'),Figure_Name)
		h_figs=get(0,'children');
		h_MainFig = findobj(h_figs,'flat',...
				    'name',Figure_Name);

	%If the figure does not exist, initialize it and then
	%run the function that was originally intended

		if length(h_MainFig)==0
			ImportXL('init');
			ImportXL(fn);
			return;
		end;
	end;
end;
%____________________________________________________________________________

%Function: CheckBox

%This function is to make sure only one check box is active at a time.
%Choice1, Choice2, and Choice3 are strings passed through argument and
%correspond to different choice buttons.

if strcmp(fn,'CheckBox')

	h_data=get(h_MainFig,'User');
	h_Button(1:3)=h_data(1:3);

	if strcmp(arg,'Choice1')

		set(h_Button(1),'value',1);
		set(h_Button(2:3),'value',0);

	elseif strcmp(arg,'Choice2')

		set(h_Button(2),'value',1);
		set(h_Button([1 3]),'value',0);

	else

		set(h_Button(3),'value',1);
		set(h_Button(1:2),'value',0);

	end;
		
end;

%____________________________________________________________________________

%Function: Cancel

%This function deletes all the figure windows initialized in this function

if strcmp(fn,'Cancel')

	h_data=get(h_MainFig,'user');

	h_Figs=findobj('Tag','Import XL');

	delete(h_Figs);
	return;

end;
%____________________________________________________________________________

%Function: Fig1.1

if strcmp(fn,'Fig1.1')

	h_data=get(h_MainFig,'User');

	h_Fig11=h_data(4);

	set(h_MainFig,'Visible','Off');
	set(h_Fig11,'pos',get(h_MainFig,'pos'),...
	'Visible','on');
	return;

end;
%____________________________________________________________________________

%Function: Back_to_MainFig

%Sets Figures 1.1, 2.1, 3.1 visible to off.  Can be used for all of them.

if strcmp(fn,'Back_to_MainFig')

	h_data=get(h_MainFig,'User');

	h_Figs(1)=h_data(4);

	set(h_MainFig,'pos',get(gcf,'pos'),...
	'visible','on')
	set(h_Figs,'visible','off');
	return;

end;
%____________________________________________________________________________

%Function:Fig1.2

if strcmp(fn,'Fig1.2')

	h_data=get(h_MainFig,'User');
	h_Fig11=h_data(4);
	h_Fig12=h_data(6);
	
	h_data11=get(h_Fig11,'User');
	h_XLFile=h_data11(1);
	h_MessageText=h_data11(2);
	
	

	c_link=ddeinit('excel',get(h_XLFile,'string'));

	

	if c_link==0

		disp('Fail');

	else

		set(h_Fig12,'pos',get(h_Fig11,'pos'),...
			    'visible','on');
		set(h_Fig11,'visible','off');

	end;

	h_data(7)=c_link;
	set(h_MainFig,'User',h_data);

	return;

end;
%____________________________________________________________________________

%Function: Back_to_1.1

%This function is run when the User pushes the back button on figure 1.2

if strcmp(fn,'Back_to_1.1')

	h_data=get(h_MainFig,'User');

	h_Fig12=h_data(6);
	h_Fig11=h_data(4);
	c_link=h_data(7);

	ddeterm(c_link);
	set(h_Fig11,'pos',get(h_Fig12,'pos'),...
		    'visible','on');
	set(h_Fig12,'visible','off');

	return;

end;

%____________________________________________________________________________

%Function: Fig1.3

if strcmp(fn,'Fig1.3')

	h_data=get(h_MainFig,'User');
	h_Fig12=h_data(6);
	h_Fig13=h_data(8);

	h_data12=get(h_Fig12,'User');
	h_StartCol=h_data12(1);
	h_FinishCol=h_data12(2);
	h_StartRow=h_data12(3);
	h_FinishRow=h_data12(4);

	set(h_Fig13,'pos',get(gcf,'pos'),...
		    'visible','on');

	set(h_Fig12,'visible','off');
	
	StartCol=get(h_StartCol,'string');
	FinishCol=get(h_FinishCol,'string');
	StartRow=num2str(get(h_StartRow,'string'));
	FinishRow=num2str(get(h_FinishRow,'string'));
	
	
	if (abs(StartCol) >= 97) & (abs(StartCol) <= 122)

		StartCol=StartCol-32;

	elseif StartCol~=['A':'Z']

		disp('Error');
		return;

	end;

	if (abs(FinishCol) >= 97) & (abs(FinishCol) <= 122)

		FinishCol=FinishCol-32;

	elseif FinishCol~=['A':'Z']

		disp('Error');
		return;

	end;

	StartCol=abs(StartCol)-64;
	FinishCol=abs(FinishCol)-64;

	AreaString=['r' num2str(StartRow) 'c' num2str(StartCol) ':r' num2str(FinishRow) 'c' num2str(FinishCol)];
	
	c_link=h_data(7);

	ReturnValue=ddereq(c_link,AreaString);

	
	
	return;

	

end;
%____________________________________________________________________________

%Function: Back_to_1.2

%This function is run when the User pushes the back button on figure 1.2

if strcmp(fn,'Back_to_1.2')

	h_data=get(h_MainFig,'User');

	h_Fig12=h_data(6);
	h_Fig13=h_data(8);
	c_link=h_data(7);

	set(h_Fig12,'pos',get(h_Fig13,'pos'),...
		    'visible','on');
	set(h_Fig13,'visible','off');

	return;

end;

%____________________________________________________________________________

%Function: Finish

if strcmp(fn,'Finish')

	h_data=get(h_MainFig,'User');
	h_Fig13=h_data(8);
	
	h_data13=get(h_Fig13,'User');
	h_VariableName=h_data13(1);
	h_SaveLink=h_data13(2);

	if ~get(h_SaveLink,'Value')

		global c_link;
		c_link=0;

	end;

	VariableName=get(h_VariableName,'string');

	if length(VariableName) == 0

		return;
	end;


	DeclareString=['global ' VariableName];
	eval(DeclareString);
	
	return;

end;
%____________________________________________________________________________


if strcmp(fn,'init')

%Delete any figures from an unclean exit

	delete(findobj('Tag','Import XL'));


%Top Figure-------------------------------------------------------------

	h_MainFig=figure('pos',[175 250 300 160],...
			 'NumberTitle','off',...
			 'MenuBar','none',...
			 'color',[.76 .76 .76],...
			 'Name',Figure_Name,...
			 'Tag','Import XL');
	
	uicontrol('pos',[80 130 140 18],...
	'back',[.76 .76 .76],...
	'string','Chose one of the following',...
	'style','text');
	
	h_Button1=uicontrol('pos',[50 100 200 18],...
			    'style','RadioButton',...
			    'string','Excel is open and data is entered',...
			    'back',[.76 .76 .76],...
			    'CallBack','ImportXL(''CheckBox'',''Choice1'');',...
			    'value',1);

	h_Button2=uicontrol('pos',[50 80 200 18],...
			    'style','RadioButton',...
			    'string','Data is saved to a file',...
			    'back',[.76 .76 .76],...
			    'enable','off',...
			    'CallBack','ImportXL(''CheckBox'',''Choice2'');',...
			    'value',0);

	h_Button3=uicontrol('pos',[50 60 200 18],...
			    'style','RadioButton',...
			    'string','Data is not yet entered',...
			    'back',[.76 .76 .76],...
			    'enable','off',...
			    'CallBack','ImportXL(''CheckBox'',''Choice3'');',...
			    'value',0);


	uicontrol('pos',[15 15 80 25],...
	'string','Cancel',...
	'CallBack','ImportXL(''Cancel'');');

	h_TopNext=uicontrol('pos',[205 15 80 25],...
	'string','Next   >',...
	'CallBack','ImportXL(''Fig1.1'');');

%Figure 1.1-----------------------------------------------------

	h_Fig11=figure('pos',[175 250 300 160],...
		'NumberTitle','off',...
		'MenuBar','none',...
		'color',[.76 .76 .76],...
		'Name','Import Excel Data 1 of 3',...
		'visible','off',...
		'Tag','Import XL');

	h_XLFile=uicontrol(h_Fig11,...
		'pos',[80 80 140 18],...
		'back',[1 1 1],...
		'Style','Edit');

	uicontrol(h_Fig11,...
	'pos',[79 79 142 20],...
	'back',[1 1 1],...
	'Style','Frame');

	h_MessageText=uicontrol(h_Fig11,...
	'pos',[50 110 200 18],...
	'back',[.76 .76 .76],...
	'Style','Text',...
	'String','Enter name of current Excel file');

	uicontrol(h_Fig11,...
	'pos',[15 15 80 25],...
	'string','<   Back',...
	'CallBack','ImportXL(''Back_to_MainFig'');');

	uicontrol(h_Fig11,...
	'pos',[205 15 80 25],...
	'string','Next   >',...
	'CallBack','ImportXL(''Fig1.2'');');
	

%Figure 1.2-----------------------------------------------------

	
	h_Fig12=figure('pos',[175 250 300 160],...
		'NumberTitle','off',...
		'MenuBar','none',...
		'color',[.76 .76 .76],...
		'Name','Import Excel Data 2 of 3',...
		'visible','off',...
		'Tag','Import XL');

	uicontrol(h_Fig12,...
	'pos',[15 15 80 25],...
	'string','<   Back',...
	'CallBack','ImportXL(''Back_to_1.1'');');

	uicontrol(h_Fig12,...
	'pos',[205 15 80 25],...
	'string','Next   >',...
	'CallBack','ExcelMatrix=ImportXL(''Fig1.3'');');

	uicontrol(h_Fig12,...
	'pos',[50 135 200 17],...
	'string','Enter Letter of Start/Finish Column',...
	'style','text',...
	'back',[.76 .76 .76]);

	uicontrol(h_Fig12,...
	'pos',[56 110 40 17],...
	'string','Start',...
	'style','text',...
	'back',[.76 .76 .76]);

	h_StartCol=uicontrol(h_Fig12,...
	'pos',[100 110 15 17],...
	'style','edit',...
	'back',[1 1 1]);

	uicontrol(h_Fig12,...
	'pos',[96 108 20 24],...
	'style','frame',...
	'back',[1 1 1]);

	uicontrol(h_Fig12,...
	'pos',[160 110 40 17],...
	'string','Finish',...
	'style','text',...
	'back',[.76 .76 .76]);

	h_FinishCol=uicontrol(h_Fig12,...
	'pos',[205 110 15 17],...
	'style','edit',...
	'back',[1 1 1]);
	
	uicontrol(h_Fig12,...
	'pos',[201 108 20 24],...
	'style','frame',...
	'back',[1 1 1]);

	uicontrol(h_Fig12,...
	'pos',[50 85 200 17],...
	'string','Enter Number of Start/Finish Row',...
	'style','text',...
	'back',[.76 .76 .76]);

	uicontrol(h_Fig12,...
	'pos',[56 60 40 17],...
	'string','Start',...
	'style','text',...
	'back',[.76 .76 .76]);

	h_StartRow=uicontrol(h_Fig12,...
	'pos',[100 60 15 17],...
	'style','edit',...
	'back',[1 1 1]);

	uicontrol(h_Fig12,...
	'pos',[96 58 20 24],...
	'style','frame',...
	'back',[1 1 1]);

	uicontrol(h_Fig12,...
	'pos',[160 60 40 17],...
	'string','Finish',...
	'style','text',...
	'back',[.76 .76 .76]);

	h_FinishRow=uicontrol(h_Fig12,...
	'pos',[205 60 15 17],...
	'style','edit',...
	'back',[1 1 1]);
	
	uicontrol(h_Fig12,...
	'pos',[201 58 20 24],...
	'style','frame',...
	'back',[1 1 1]);

	
%Figure 1.3-----------------------------------------------------
	
	
	h_Fig13=figure('pos',[175 250 300 160],...
		'NumberTitle','off',...
		'MenuBar','none',...
		'color',[.76 .76 .76],...
		'Name','Import Excel Data 3 of 3',...
		'visible','off',...
		'Tag','Import XL');

	uicontrol(h_Fig13,...
	'pos',[15 15 80 25],...
	'string','<   Back',...
	'CallBack','ImportXL(''Back_to_1.2'');');

	uicontrol(h_Fig13,...
	'pos',[205 15 80 25],...
	'string','Finish',...
	'CallBack',['ImportXL(''Finish'');'...
		    'Finishup;']);

	
	h_VariableName=uicontrol(h_Fig13,...
	'pos',[50 100 200 17],...
	'back',[1 1 1],...
	'style','edit');

	uicontrol(h_Fig13,...
	'pos',[46 99 205 21],...
	'back',[1 1 1],...
	'style','frame');

	uicontrol(h_Fig13,...
	'pos',[50 130 200 20],...
	'back',[.76 .76 .76],...
	'style','text',...
	'string','Type name of variable');

	h_SaveVariable=uicontrol(h_Fig13,...
	'pos',[60 60 200 20],...
	'style','RadioButton',...
	'back',[.76 .76 .76],...
	'string','Save channel handle in variable c_link');



	figure(h_MainFig);

	h_data(1)=h_Button1;
	h_data(2)=h_Button2;
	h_data(3)=h_Button3;
	h_data(4)=h_Fig11;
	h_data(5)=h_TopNext;
	h_data(6)=h_Fig12;
	h_data(7)=0;		%This is for the link channel handle
	h_data(8)=h_Fig13;

	h_data11(1)=h_XLFile;
	h_data11(2)=h_MessageText;

	h_data12(1)=h_StartCol;
	h_data12(2)=h_FinishCol;
	h_data12(3)=h_StartRow;
	h_data12(4)=h_FinishRow;

	h_data13(1)=h_VariableName;
	h_data13(2)=h_SaveVariable;

	set(h_MainFig,'User',h_data);
	set(h_Fig11,'User',h_data11);
	set(h_Fig12,'User',h_data12);
	set(h_Fig13,'User',h_data13);

end;
