Matlab课程设计报告(简单计算器).docx
《Matlab课程设计报告(简单计算器).docx》由会员分享,可在线阅读,更多相关《Matlab课程设计报告(简单计算器).docx(19页珍藏版)》请在冰豆网上搜索。
![Matlab课程设计报告(简单计算器).docx](https://file1.bdocx.com/fileroot1/2022-12/7/b739979e-da6b-4324-934b-de90442c9e13/b739979e-da6b-4324-934b-de90442c9e131.gif)
1、设计目的
运用MATLAB实现MATLAB的GUI程序设计。
2、题目分析
2.1课程设计的基本要求:
A.熟悉和掌握MATLAB程序设计方法。
B.掌握MATLABGUI程序设计。
2.2课程设计的内容
要求利用MATLABGUI设计实现一个图形用户界面的计算器程序,要求实现:
A.具有友好的用户图形界面。
实现十进制数的加、减、乘、除、乘方、取模等简单计算。
B.科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等函数运行。
C. 能够保存上次历史计算的答案,显示答案存储器中的内容。
D.有清除键,能清除操作。
E.独立存储器功能,使之可以直接输入存储器,可与存储器中的数值相加减。
能够清除独立存储器中的内容。
2.3题目分析
本题目通过MATLAB的GUI程序设计较为简单,在GUI设计中主要用到三种控件,显示框用到文本编辑框(edittext),说明框用到静态文本框(Statictext),数字以及运算等按钮用到命令按钮(pushbutton)。
然后再通过各个按钮的回调函数,实现简单的计算功能。
3、总体设计
首先用MATLABGUI功能,在绘制一个静态文本框和一个文本编辑框,以及32个命令按钮,调整好各控件大小、颜色,整体布局如图所示:
然后通过双击个按钮来改写其属性,在m文件中编写其回调函数,最后在运行调试。
4、具体设计
4.1各功能界面设计
GUI设计界面:
4.2各功能模块实现
算法设计:
A.数字键设计:
0—9以及小数点函数都一样,只是参数不同:
global jj
textString=get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','1');
else
textString=strcat(textString,'1');
set(handles.text1,'String',textString)
end
jj=0;
B.四则运算函数:
textString=get(handles.text1,'String');
textString=strcat(textString,'+');
set(handles.text1,'String',textString)
C.科学计算函数:
textString=get(handles.text1,'String');
if(strcmp(textString,'0.')==1)
set(handles.text1,'String','0.');
else
a=strread(textString, '%f');
a=sin(a);
set(handles.text1,'String',a)
end
或
textString=handles.text1;
textString=sin(str2num(get(handles.text1,'String'))*pi/180);
set(handles.text1,'String',num2str(textString))
D.退格键:
通过取屏幕值,计算出其字符长度,然后取其前N-1项的值来实现退格:
global jj
textString=get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','0.');
else
ss=char(textString);
l=length(textString);
textString=ss(1:
l-1);
set(handles.text1,'String',textString)
end
jj=0;
E.清屏键函数:
set(handles.text1,'String','0.');
F.右键函数:
gtext('大家好;我是智能机器人-my nameisseven');
close(gcf);
4.3各模块实现结果
A.数字键:
B.四则运算函数:
C.科学计算函数:
Sin45的计算结果=
经过计算,这些结果均与实际结果相吻合,计算器的功能实现的较为完好。
5.2问题和解决方法:
a. 小数点可以连续输入。
解决方法是:
用strfind函数查看文本框里有几个小数点,如果已经有一个了,再按小数点就保持不变。
b. 按过运算符号后一个数不等于一个数,比如:
输入1,按等号,会出来一个3,经过长时间分析得知,这是由于在按运算符号时,系统记录了文本框里的数但没有清空,才会出现这种问题。
解决方法是再申请一个不同于加减乘除的另一个符号,并将按过运算符后记录的数值置0。
c. 按对数函数键时,负数也能运算,通过加入if判断语句来判断输入的值是否为负,若为负则输出error.
6、心得体会
通过本次的MATLAB课程设计,让我对MATLAB尤其是其GUI设计的功能有了进一步的了解,认识到了它功能的强大。
在MATLAB简单计算器的设计中,了解了关于MATLAB图形用户界面的部分控件的使用方法;利用MATLAB的GUI提供的很多实用的控件,方便用于设计属于自己的图形界面。
7、附录(源代码)
function varargout=jisuanqi(varargin)
%JISUANQIM-fileforjisuanqi.fig
%JISUANQI,byitself,createsanewJISUANQIorraisestheexisting
%singleton*.
%
%H=JISUANQIreturnsthehandletoanewJISUANQIorthehandleto
%theexistingsingleton*.
%
%JISUANQI('CALLBACK',hObject,eventData,handles,...)callsthelocal
%functionnamedCALLBACKinJISUANQI.Mwiththegiveninputarguments.
%
%JISUANQI('Property','Value',...)createsanewJISUANQIorraisesthe
%existingsingleton*.Startingfromtheleft,propertyvaluepairsare
%appliedtotheGUIbeforejisuanqi_OpeningFunctiongetscalled.An
%unrecognizedpropertynameorinvalidvaluemakespropertyapplication
%stop.Allinputsarepassedtojisuanqi_OpeningFcnviavarargin.
%
%*SeeGUIOptionsonGUIDE'sToolsmenu.Choose"GUIallowsonlyone
%instancetorun(singleton)".
%
%Seealso:
GUIDE,GUIDATA,GUIHANDLES
%Copyright2002-2003TheMathWorks,Inc.
%Edittheabovetexttomodifytheresponsetohelpjisuanqi
%LastModifiedbyGUIDEv2.504-Dec-201217:
06:
43
%Begininitializationcode-DONOTEDIT
gui_Singleton=1;
gui_State=struct('gui_Name',mfilename, ...
'gui_Singleton',gui_Singleton, ...
'gui_OpeningFcn',@jisuanqi_OpeningFcn, ...
'gui_OutputFcn',@jisuanqi_OutputFcn, ...
'gui_LayoutFcn',[], ...
'gui_Callback',[]);
if nargin&&ischar(varargin{1})
gui_State.gui_Callback=str2func(varargin{1});
end
if nargout
[varargout{1:
nargout}]=gui_mainfcn(gui_State,varargin{:
});
else
gui_mainfcn(gui_State,varargin{:
});
end
%Endinitializationcode-DONOTEDIT
%---Executesjustbeforejisuanqiismadevisible.
function jisuanqi_OpeningFcn(hObject,eventdata,handles,varargin)
%Thisfunctionhasnooutputargs,seeOutputFcn.
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%varargincommandlineargumentstojisuanqi(seeVARARGIN)
%Choosedefaultcommandlineoutputforjisuanqi
handles.output=hObject;
%Updatehandlesstructure
guidata(hObject,handles);
%UIWAITmakesjisuanqiwaitforuserresponse(seeUIRESUME)
%uiwait(handles.figure1);
%定义全局变量jj用于数字的设定
global jj;
%设置句柄,用于将按键接收的值返回给主程序
set(handles.text1,'String','0.');
jj=0;
%---Outputsfromthisfunctionarereturnedtothecommandline.
function varargout=jisuanqi_OutputFcn(hObject,eventdata,handles)
%varargoutcellarrayforreturningoutputargs(seeVARARGOUT);
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%Getdefaultcommandlineoutputfromhandlesstructure
varargout{1}=handles.output;
function edit1_Callback(hObject,eventdata,handles)
%hObjecthandletoedit1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%Hints:
get(hObject,'String')returnscontentsofedit1astext
%str2double(get(hObject,'String'))returnscontentsofedit1asadouble
%---Executesduringobjectcreation,aftersettingallproperties.
function edit1_CreateFcn(hObject,eventdata,handles)
%hObjecthandletoedit1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesempty-handlesnotcreateduntilafterallCreateFcnscalled
%Hint:
editcontrolsusuallyhaveawhitebackgroundonWindows.
%SeeISPCandCOMPUTER.
if ispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%---Executesonbuttonpressinpushbutton1.
function pushbutton1_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
global jj
textString=get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','1');
else
textString=strcat(textString,'1');
set(handles.text1,'String',textString)
end
jj=0;
%---Executesonbuttonpressinpushbutton2.
function pushbutton2_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton2(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
global jj
textString=get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','2');
else
textString=strcat(textString,'2');
set(handles.text1,'String',textString)
end
jj=0;
%---Executesonbuttonpressinpushbutton3.
function pushbutton3_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton3(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
global jj
textString=get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','3');
else
textString=strcat(textString,'3');
set(handles.text1,'String',textString)
end
jj=0;
%---Executesonbuttonpressinpushbutton13.
function pushbutton13_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton13(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
global jj
textString=get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','+');
else
textString=get(handles.text1,'String');
textString=strcat(textString,'+');
set(handles.text1,'String',textString)
end
jj=0;
%---Executesonbuttonpressinpushbutton14.
function pushbutton14_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton14(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
global jj
textString=get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','-');
else
textString=get(handles.text1,'String');
textString=strcat(textString,'-');
set(handles.text1,'String',textString)
end
jj=0;
%---Executesonbuttonpressinpushbutton21.
function pushbutton21_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton21(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%textString=get(handles.text1,'String');
%strcmp(textString,'0.')
%(if(strcmp(textString,'0.')==1)
%%else
%a=strread(textString,'%f');
%a=a*a;
%set(handles.text1,'String',a)
%end)
textString=get(handles.text1,'String')
textString=strcat(textString,'^2')
set(handles.text1,'String',textString)
%---Executesonbuttonpressinpushbutton22.
function pushbutton22_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton22(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
textString=get(handles.text1,'String');
%strcmp(textString,'0.')
textString=handles.text1;
textString=sin(str2num(get(handles.text1,'String'))*pi/180);
set(handles.text1,'String',num2str(textString))
%a=strread(textString,'%f')
%textString=get(handles.text1,'String')
%textString=strcat(textString,'sin')
%set(handles.text1,'String',textString)
%---Executesonbuttonpressinpushbutton23.
function pushbutton23_Callback(hObject,eventdata,handles)
%hObjecthand