电子信息工程系实验报告课程名称:MATLAB 应用实验项目名称:GUI 计算器设计实验时间:2012-11-22班级:测控081姓名:学号:810707132实验目的:1.熟悉MATLAB 的菜单设计方法2.熟悉MATLAB 的主要控件使用方法3.熟悉MATLAB 的GUI 设计流程4.运用MATLAB 的GUI 设计一个简单的计算器实验环境:MATLAB7.8实验内容:function varargout =jisuanqi1(varargin)%JISUANQI1M-file for jisuanqi1.fig %JISUANQI1,by itself,creates a new JISUANQI1or raises the existing %singleton*.%%H =JISUANQI1returns the handle to a new JISUANQI1or the handle to %the existing singleton*.%%JISUANQI1('CALLBACK',hObject,eventData,handles,...)calls the local %function named CALLBACK in JISUANQI1.M with the given input arguments.%%JISUANQI1('Property','Value',...)creates a new JISUANQI1or raises the %existing singleton*.Starting from the left,property value pairs are %applied to the GUI before jisuanqi1_OpeningFunction gets called.An %unrecognized property name or invalid value makes property application %stop.All inputs are passed to jisuanqi1_OpeningFcn via varargin.%%*See GUI Options on GUIDE's Tools menu.Choose "GUI allows only one %instance to run (singleton)".%%See also:GUIDE,GUIDATA,GUIHANDLES%Edit the above text to modify the response to help jisuanqi1%Last Modified by GUIDE v2.505-Dec-201022:24:59%Begin initialization code -DO NOT EDIT gui_Singleton =1;成绩:指导教师(签名):'gui_Singleton',gui_Singleton,...'gui_OpeningFcn',@jisuanqi1_OpeningFcn,...'gui_OutputFcn',@jisuanqi1_OutputFcn,...'gui_LayoutFcn',[],...'gui_Callback',[]);if nargin&&ischar(varargin{1})gui_State.gui_Callback=str2func(varargin{1});endif nargout[varargout{1:nargout}]=gui_mainfcn(gui_State,varargin{:}); elsegui_mainfcn(gui_State,varargin{:});end%End initialization code-DO NOT EDIT%---Executes just before jisuanqi1is made visible.function jisuanqi1_OpeningFcn(hObject,eventdata,handles,varargin) %This function has no output args,see OutputFcn.%hObject handle to figure%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%varargin command line arguments to jisuanqi1(see VARARGIN)%以下是本实验初始化部分程序的代码:global c p t c1f;f=0;t=0;p=2;c1=num2str(0);c='0.';set(handles.edit1,'String',c);guidata(hObject,handles);%Choose default command line output for jisuanqi1handles.output=hObject;%Update handles structureguidata(hObject,handles);%UIWAIT makes jisuanqi1wait for user response(see UIRESUME)%uiwait(handles.figure1);%---Outputs from this function are returned to the command line. function varargout=jisuanqi1_OutputFcn(hObject,eventdata,handles)%hObject handle to figure%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%Get default command line output from handles structure varargout{1}=handles.output;%---Executes on button press in pushbutton3.function pushbutton3_Callback(hObject,eventdata,handles)%hObject handle to pushbutton3(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验数字‘8’的程序代码:global t f c;if f=='=';c='0';c=8+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elseif t==0;c=8+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elsec=strcat(c,'8');set(handles.edit1,'String',c);guidata(hObject,handles);t=t+1;end%---Executes on button press in pushbutton4.function pushbutton4_Callback(hObject,eventdata,handles)%hObject handle to pushbutton4(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验数字‘5’的程序代码:global t f c;if f=='=';c='0';c=5+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);elseif t==0;c=5+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elsec=strcat(c,'5');set(handles.edit1,'String',c);guidata(hObject,handles);t=t+1;end%---Executes on button press in pushbutton5.function pushbutton5_Callback(hObject,eventdata,handles)%hObject handle to pushbutton5(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验数字‘2’的程序代码:global t f c;if f=='=';c='0';c=2+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elseif t==0;c=2+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elsec=strcat(c,'2');set(handles.edit1,'String',c);guidata(hObject,handles);t=t+1;end%---Executes on button press in pushbutton6.function pushbutton6_Callback(hObject,eventdata,handles)%hObject handle to pushbutton6(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘+/-’的程序代码:global c t p;c=strcat('-',c);set(handles.edit1,'String',c);guidata(hObject,handles);p=p+1;elsec=strrep(c,'-','');set(handles.edit1,'String',c);guidata(hObject,handles);p=p+1;end%---Executes on button press in pushbutton7.function pushbutton7_Callback(hObject,eventdata,handles)%hObject handle to pushbutton7(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘.’的程序代码:global c t;t=1;c=strcat(c,'.');set(handles.edit1,'String',c);guidata(hObject,handles);%---Executes on button press in pushbutton8.function pushbutton8_Callback(hObject,eventdata,handles)%hObject handle to pushbutton8(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验数字‘3’的程序代码:global t f c;if f=='=';c='0';c=3+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elseif t==0;c=3+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elsec=strcat(c,'3');set(handles.edit1,'String',c);t=t+1;end%---Executes on button press in pushbutton9.function pushbutton9_Callback(hObject,eventdata,handles)%hObject handle to pushbutton9(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验数字‘6’的程序代码:global t f c;if f=='=';c='0';c=6+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elseif t==0;c=6+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elsec=strcat(c,'6');set(handles.edit1,'String',c);guidata(hObject,handles);t=t+1;end%---Executes on button press in pushbutton10.function pushbutton10_Callback(hObject,eventdata,handles)%hObject handle to pushbutton10(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验数字‘9’的程序代码:global t f c;if f=='=';c='0';c=9+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elseif t==0;c=9+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);elsec=strcat(c,'9');set(handles.edit1,'String',c);guidata(hObject,handles);t=t+1;end%---Executes on button press in pushbutton11.function pushbutton11_Callback(hObject,eventdata,handles)%hObject handle to pushbutton11(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘+’的程序代码:global c c1f t;if f=='-';c1=str2num(c1)-str2num(c);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='+';elseif f=='/'&str2num(c)==0;c2='³ýÊý²»ÄÜΪÁã¡£'set(handles.edit1,'String',c2);guidata(hObject,handles);c=num2str(0);c1=num2str(0);f='=';elseif f=='/';c1=str2num(c1)/str2num(c);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='+';elseif f=='*';c1=str2num(c)*str2num(c1);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='+';elsec1=str2num(c)+str2num(c1);c1=num2str(c1);guidata(hObject,handles);c=num2str(0);f='+';endt=0;%---Executes on button press in pushbutton12.function pushbutton12_Callback(hObject,eventdata,handles)%hObject handle to pushbutton12(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘-’的程序代码:global c c1f t;if(c1==num2str(0)&f==0)|(c1==num2str(0)&f=='=')c1=str2num(c)-str2num(c1);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='-';elseif f=='+';c1=str2num(c)+str2num(c1);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='-';elseif f=='/'&str2num(c)==0;c2='³ýÊý²»ÄÜΪÁã¡£'set(handles.edit1,'String',c2);guidata(hObject,handles);c=num2str(0);c1=num2str(0);f='=';elseif f=='/';c1=str2num(c1)/str2num(c);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='-';elseif f=='*';c1=str2num(c)*str2num(c1);c1=num2str(c1);set(handles.edit1,'String',c1);c=num2str(0);f='-';elsec1=str2num(c1)-str2num(c);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='-';endt=0;%---Executes on button press in pushbutton13.function pushbutton13_Callback(hObject,eventdata,handles)%hObject handle to pushbutton13(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘*’的程序代码:global c c1f t;if f=='-';c1=str2num(c1)-str2num(c);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='*';elseif f=='+';c1=str2num(c)+str2num(c1);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='*';elseif f=='/'&str2num(c)==0;c2='³ýÊý²»ÄÜΪÁã¡£'set(handles.edit1,'String',c2);guidata(hObject,handles);c=num2str(0);c1=num2str(0);f='=';elseif f=='/';c1=str2num(c1)/str2num(c);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);f='*';elseif(c1==num2str(0)&f==0)|(c1==num2str(0)&f=='=');c1=str2num(c)*(str2num(c1)+1);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='*';elsec1=str2num(c)*str2num(c1);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='*';endt=0;%---Executes on button press in pushbutton14.function pushbutton14_Callback(hObject,eventdata,handles)%hObject handle to pushbutton14(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘/’的程序代码:global c c1f t;if f=='-';c1=str2num(c1)-str2num(c);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='/';elseif f=='+';c1=str2num(c)+str2num(c1);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='/';elseif f=='/'&str2num(c)==0;c2='³ýÊý²»ÄÜΪÁã¡£'set(handles.edit1,'String',c2);guidata(hObject,handles);c=num2str(0);c1=num2str(0);elseif f=='*';c1=str2num(c)*str2num(c1);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='/';elseif(c1==num2str(0)&f==0)|(c1==num2str(0)&f=='=');c1=str2num(c)/(str2num(c1)+1);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='/';elsec1=str2num(c1)/str2num(c);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);f='/';endt=0;%---Executes on button press in pushbutton15.function pushbutton15_Callback(hObject,eventdata,handles)%hObject handle to pushbutton15(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验数字‘0’的程序代码:global t f c;if f=='=';c='0';c=0+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elseif t==0;c=0+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elsec=strcat(c,'0');set(handles.edit1,'String',c);t=t+1;end%---Executes on button press in pushbutton16.function pushbutton16_Callback(hObject,eventdata,handles)%hObject handle to pushbutton16(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验数字‘1’的程序代码:global t f c;if f=='=';c='0';c=1+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elseif t==0;c=1+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elsec=strcat(c,'1');set(handles.edit1,'String',c);guidata(hObject,handles);t=t+1;end%---Executes on button press in pushbutton17.function pushbutton17_Callback(hObject,eventdata,handles)%hObject handle to pushbutton17(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验数字‘4’的程序代码:global t f c;if f=='=';c='0';c=4+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elseif t==0;c=4+str2num(c)*10;set(handles.edit1,'String',c);guidata(hObject,handles);elsec=strcat(c,'4');set(handles.edit1,'String',c);guidata(hObject,handles);t=t+1;endfunction edit1_Callback(hObject,eventdata,handles)%hObject handle to edit1(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB%handles structure with handles and user data(see GUIDATA)%Hints:get(hObject,'String')returns contents of edit1as text%str2double(get(hObject,'String'))returns contents of edit1as a double%---Executes during object creation,after setting all properties. function edit1_CreateFcn(hObject,eventdata,handles)%hObject handle to edit1(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB%handles empty-handles not created until after all CreateFcns called%Hint:edit controls usually have a white background on Windows.%See ISPC and COMPUTER.if ispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end%---Executes on button press in pushbutton18.function pushbutton18_Callback(hObject,eventdata,handles)%hObject handle to pushbutton18(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB%handles structure with handles and user data(see GUIDATA)%以下是本实验数字‘7’的程序代码:global t f c;if f=='=';c='0';c=7+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);elseif t==0;c=7+str2num(c)*10;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);elsec=strcat(c,'7');set(handles.edit1,'String',c);guidata(hObject,handles);t=t+1;end%---Executes on button press in pushbutton19.function pushbutton19_Callback(hObject,eventdata,handles)%hObject handle to pushbutton19(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘CE’的程序代码:global c c1f;f=0;c1=num2str(0);c=0;c=num2str(c);set(handles.edit1,'String',c);guidata(hObject,handles);c=num2str(0);t=0;%---Executes on button press in pushbutton20.function pushbutton20_Callback(hObject,eventdata,handles)%hObject handle to pushbutton20(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘=’的程序代码:global c c1f t;if f=='-';c1=str2num(c1)-str2num(c);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);elseif f=='/'&str2num(c)==0;c2='³ýÊý²»ÄÜΪÁã¡£'guidata(hObject,handles);c=num2str(0);f=0;elseif f=='/';c1=str2num(c1)/str2num(c);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);elseif f=='*';c1=str2num(c)*str2num(c1);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);elsec1=str2num(c)+str2num(c1);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);endt=0;f='=';c=c1;c1=num2str(0);%---Executes on button press in pushbutton21.function pushbutton21_Callback(hObject,eventdata,handles)%hObject handle to pushbutton21(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%---Executes on button press in pushbutton22.function pushbutton22_Callback(hObject,eventdata,handles)%hObject handle to pushbutton22(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘sqrt’的程序代码:global c c1f t;c1=sqrt(str2num(c))c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=c1;c1=num2str(0);t=0;f='=';%---Executes on button press in pushbutton23.function pushbutton23_Callback(hObject,eventdata,handles)%hObject handle to pushbutton23(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘π’的程序代码:global c;c=num2str(pi);set(handles.edit1,'String','¦Ð');guidata(hObject,handles);%---Executes on button press in pushbutton24.function pushbutton24_Callback(hObject,eventdata,handles)%hObject handle to pushbutton24(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘Backspace’的程序代码:global c t;if t==0;c=floor(str2num(c)/10);c=num2str(c);elseif t==2;c=floor(str2num(c));c=num2str(c);c=strcat(c,'.');t=t-1;elseif t==1;c=floor(str2num(c));c=num2str(c);t=t-1;elsec=(floor(str2num(c)*10^(t-1)/10))*10^(-t+2);c=num2str(c);t=t-1;endset(handles.edit1,'String',c);guidata(hObject,handles);%---Executes on button press in pushbutton25.%hObject handle to pushbutton25(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘1/x’的程序代码:global c c1f t;if c==num2str(0)|c1==num2str(0);c2='³ýÊý²»ÄÜΪÁã¡£'set(handles.edit1,'String',c2);guidata(hObject,handles);elsec1=1/str2num(c);c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);endc=num2str(0);c=c1;c1=num2str(0);t=0;f='=';%---Executes on button press in pushbutton26.function pushbutton26_Callback(hObject,eventdata,handles)%hObject handle to pushbutton26(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB %handles structure with handles and user data(see GUIDATA)%以下是本实验‘exp’的程序代码:global c c1f t;c1=exp(str2num(c))c1=num2str(c1);set(handles.edit1,'String',c1);guidata(hObject,handles);c=num2str(0);c=c1;c1=num2str(0);t=0;f='=';实验结果及分析:实验心得:这次实验做的是电脑附件里的计算器,通过该实验我更熟悉掌握了MATLAB中GUI各控件使用方法及应用,最后并利用GUI设计出了计算器,这些让我对GUI页面中的各控件的功能有了进一步的了解。