function gui_excel()
%MATLAB example to demonstrate the GUI and export the values to spreadsheet.
%The sample registration form will get the data and store it in a excel sheet.
%NOTE: Do not open sample.xls while executing because data inconsistency may occur.
%While executing, a warning message will appear to notify the user that
%addition of new spreadsheet by name SAMPLE FORM is created.
%Delete the file if exists already.
if('sample.xls')
delete sample.xls;
end
%To clear all the values present in the global varibles.
clear all;
global file_handler file_string Nxt dd mm year age_today;
file_handler=2;
%Export the column names to the excel sheet.
A={'NAME','DATE OF BIRTH','AGE','ADDRESS','EMAIL ID','PHONE NUMBER'};
%Here sample.xls is created and it will be in the MATLAB current directory
xlswrite('sample.xls',A,'SAMPLE FORM','A1');%filename, matrix,sheetname and the range is specified
figure('units','pixels','position',[250 100 500 450],'menubar','none','numbertitle','off',...
'name','Fill the form @abhi','resize','off');
title('Database @abhi','fontsize',10,'fontname','Time New Roman','color','r');
axis off;
Rform();%call the function Rform to fill the form
function Rform(source,eventdata)
uicontrol('style','push','units','pixels','position',[15 380 150 30],...
'fontsize',8,'string','ENTER YOUR NAME');
uicontrol('style','push','units','pixels','position',[15 300 150 30],...
'fontsize',8,'string','DATE OF BIRTH');
uicontrol('style','push','units','pixels','position',[15 250 150 30],...
'fontsize',8,'string','AGE');
uicontrol('style','push','units','pixels','position',[15 200 150 30],...
'fontsize',8,'string','ADDRESS');
uicontrol('style','push','units','pixels','position',[15 100 150 30],...
'fontsize',8,'string','EMAIL ID');
uicontrol('style','push','units','pixels','position',[15 50 150 30],...
'fontsize',8,'string','PHONE NUMBER');
file_string=num2str(file_handler);
uicontrol('style','edit','units','pixels','position',[200 380 250 30],...
'fontsize',14,'callback',{@nam_call});
uicontrol('style','text','position',[200 340 50 20],'fontsize',9,'string','DD');
uicontrol('style','edit','units','pixels','position',[200 300 50 30],...
'fontsize',14,'callback',{@dob_call1});
uicontrol('style','text','position',[280 340 50 20],'fontsize',9,'string','MM');
uicontrol('style','edit','units','pixels','position',[280 300 50 30],...
'fontsize',14,'callback',{@dob_call2});
uicontrol('style','text','position',[360 340 50 20],'fontsize',9,'string','YYYY');
uicontrol('style','edit','units','pixels','position',[360 300 90 30],...
'fontsize',14,'callback',{@dob_call3});
uicontrol('style','text','units','pixels','position',[200 250 250 30],...
'fontsize',14);
uicontrol('style','edit','units','pixels','position',[200 150 250 80],...
'fontsize',14,'callback',{@add_call});
uicontrol('style','edit','units','pixels','position',[200 100 250 30],...
'fontsize',14,'callback',{@email_call});
uicontrol('style','edit','units','pixels','position',[200 50 250 30],...
'fontsize',14,'callback',{@phone_call});
okButton=uicontrol('style','push','position',[200,10,30,30],'string','OK','callback',{@finish});
Nxt=uicontrol('style','push','position',[400,10,50,30],'string','NEXT','callback',{@next_call});
set(Nxt,'Visible','off');
%writing to the excel sheet
function phone_call(source,eventdata)
pno=get(source,'String');
pnum={pno};
xlswrite('sample.xls',pnum,'SAMPLE FORM' ,strcat('F',file_string));
end
function email_call(source,eventdata)
mail=get(source,'String');
e={mail};
xlswrite('sample.xls',e,'SAMPLE FORM' ,strcat('E',file_string));
end
function add_call(source,eventdata)
address=get(source,'String');
add1={address};
xlswrite('sample.xls',add1,'SAMPLE FORM' ,strcat('D',file_string));
end
function age_call(source,eventdata)
age=get(source,'String');
end
function dob_call1(source,eventdata)
dd=get(source,'String');
end
function dob_call2(source,eventdata)
mm=get(source,'String');
end
function dob_call3(source,eventdata)
year=get(source,'String');
year1=get(eventdata,'value');
dob=strcat(dd,'-', mm,'-', year);
b={dob};
xlswrite('sample.xls',b,'SAMPLE FORM' ,strcat('B',file_string));
age_calculator();
end
function nam_call(source,eventdata)
names=get(source,'String');
nam1={names};
xlswrite('sample.xls',nam1,'SAMPLE FORM',strcat('A',file_string));
end
function finish(source,eventdata)
set(Nxt,'Visible','on');
set(okButton,'Visible','off');
end
end
%Pushbutton NEXT calls the Rform to fill another form
function next_call(source,eventdata)
set(Nxt,'Visible','off');
file_handler=file_handler+1;%handle is incremented to point the next position in the excel sheet
Rform();
end
%calcute the age automatically
function age_calculator()
now=date;
dv=datevec(now);% date vector returns in yyyy mm dd format
dd1=str2num(dd);
dd_diff=dv(3)-dd1;
mm1=str2num(mm);
mm_diff=dv(2)-mm1;
years=str2num(year);
year_diff=dv(1)-years;
if(mm_diff <= 0)
if(dd_diff <= 0)
age_today=year_diff-1;
end
else
age_today=year_diff;
end
uicontrol('style','text','units','pixels','position',[200 250 250 30],...
'fontsize',10,'string',age_today);
age1={age_today};
xlswrite('sample.xls',age1,'SAMPLE FORM' ,strcat('C',file_string));
end
end