最新matlab简易计算器Word文件下载.docx
《最新matlab简易计算器Word文件下载.docx》由会员分享,可在线阅读,更多相关《最新matlab简易计算器Word文件下载.docx(18页珍藏版)》请在冰豆网上搜索。
![最新matlab简易计算器Word文件下载.docx](https://file1.bdocx.com/fileroot1/2023-2/1/8156009e-9006-4750-b73f-cda2da6a6683/8156009e-9006-4750-b73f-cda2da6a66831.gif)
使用MATLAB的GUI接口设计一个简单的计算器。
效果图:
程序源码部分函数及相关注释:
%0~9数字键及小数点按钮代码范例
%全局变量locaval用于存储用户输入的多位数值
%全局变量gloval2用于存储待处理的第二位数值
functionpushbutton1_Callback(hObject,eventdata,handles)
globallocaval;
a=get(handles.pushbutton1,'
String'
);
locaval=strcat(locaval,a);
set(handles.text1,'
locaval);
globalgloval2
gloval2=locaval;
guidata(hObject,handles);
%运算符按钮处理“+、-、*、\”范例
%全局变量flagnum存储运算符标志
%全局变量global1用于储存第一个待处理数值
functionpushbutton10_Callback(hObject,eventdata,handles)
a=get(handles.pushbutton10,'
b=get(handles.text1,'
a);
globalflagnum
globalgloval1
globallocaval
locaval='
'
;
flagnum=1;
gloval1=b;
%取相反数按钮“+\-”代码
%算法实现:
用零减去文本框现在的值,再赋值给文本框
functionpushbutton14_Callback(hObject,eventdata,handles)
locaval=str2num(locaval);
locaval=0-locaval;
locaval=num2str(locaval);
%等号按钮运算实现
%根据flagnum运算标志用switch决策语句实现相应计算
%需注意相应数据类型的转化
functionpushbutton17_Callback(hObject,eventdata,handles)
gloval1=str2num(gloval1);
gloval2=str2num(gloval2);
case1=gloval1/gloval2;
case2=gloval1*gloval2;
case3=gloval1-gloval2;
case4=gloval1+gloval2;
case1=num2str(case1);
case2=num2str(case2);
case3=num2str(case3);
case4=num2str(case4);
switchflagnum;
case1
set(handles.text1,'
case1);
case2
case2);
case3
case3);
case4
case4);
end
guidata(hObject,handles)
%BackSpace按钮函数
MATLAB是用矩阵存储数据的,相应的可以取文本框的前N-1实现其功能
functionpushbutton19_Callback(hObject,eventdata,handles)
textString=get(handles.text1,'
if(strcmp(textString,'
0.'
)==1)
'
);
else
ss=char(textString);
l=length(textString);
textString=ss(1:
l-1);
textString)
%C清除按钮函数
%把全局变量locaval清零
functionpushbutton20_Callback(hObject,eventdata,handles)
%开平方函数
functionpushbutton22_Callback(hObject,eventdata,handles)
textString=str2num(textString);
textString=sqrt(textString);
textString=num2str(textString);
textString);
%取1/x函数
functionpushbutton24_Callback(hObject,eventdata,handles)
locaval=1/locaval;
实验心得:
!
、通过MATLAB简单计算器的设计,初步了解了关于MATLAB图形用户界面的部分控件的使用方法。
2、MATLAB的GUI提供的很多实用的控件,方便用于设计属于自己的图形界面。
源码:
functionvarargout=bt0(varargin)
%BT0M-fileforbt0.fig
%BT0,byitself,createsanewBT0orraisestheexisting
%singleton*.
%
%H=BT0returnsthehandletoanewBT0orthehandleto
%theexistingsingleton*.
%BT0('
Property'
Value'
...)createsanewBT0usingthe
%givenpropertyvaluepairs.Unrecognizedpropertiesarepassedvia
%varargintobt0_OpeningFcn.Thiscallingsyntaxproducesa
%warningwhenthereisanexistingsingleton*.
CALLBACK'
)andBT0('
hObject,...)callthe
%localfunctionnamedCALLBACKinBT0.Mwiththegiveninput
%arguments.
%*SeeGUIOptionsonGUIDE'
sToolsmenu.Choose"
GUIallowsonlyone
%instancetorun(singleton)"
.
%Seealso:
GUIDE,GUIDATA,GUIHANDLES
%Edittheabovetexttomodifytheresponsetohelpbt0
%LastModifiedbyGUIDEv2.504-May-201014:
01:
00
%Begininitializationcode-DONOTEDIT
globalgloval1;
globalgloval2;
globalflagnum;
gui_Singleton=1;
gui_State=struct('
gui_Name'
mfilename,...
gui_Singleton'
gui_Singleton,...
gui_OpeningFcn'
@bt0_OpeningFcn,...
gui_OutputFcn'
@bt0_OutputFcn,...
gui_LayoutFcn'
[],...
gui_Callback'
[]);
ifnargin&
isstr(varargin{1})
gui_State.gui_Callback=str2func(varargin{1});
ifnargout
[varargout{1:
nargout}]=gui_mainfcn(gui_State,varargin{:
});
gui_mainfcn(gui_State,varargin{:
%Endinitializationcode-DONOTEDIT
%---Executesjustbeforebt0ismadevisible.
functionbt0_OpeningFcn(hObject,eventdata,handles,varargin)
%Thisfunctionhasnooutputargs,seeOutputFcn.
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%vararginunrecognizedPropertyName/PropertyValuepairsfromthe
%commandline(seeVARARGIN)
%Choosedefaultcommandlineoutputforbt0
handles.output=hObject;
%Updatehandlesstructure
%UIWAITmakesbt0waitforuserresponse(seeUIRESUME)
%uiwait(handles.figure1);
%---Outputsfromthisfunctionarereturnedtothecommandline.
functionvarargout=bt0_OutputFcn(hObject,eventdata,handles)
%varargoutcellarrayforreturningoutputargs(seeVARARGOUT);
%Getdefaultcommandlineoutputfromhandlesstructure
varargout{1}=handles.output;
%---Executesonbuttonpressinpushbutton1.
%hObjecthandletopushbutton1(seeGCBO)
%---Executesonbuttonpressinpushbutton2.
functionpushbutton2_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton2(seeGCBO)
a=get(handles.pushbutton2,'
%---Executesonbuttonpressinpushbutton3.
functionpushbutton3_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton3(seeGCBO)
a=get(handles.pushbutton3,'
%---Executesonbuttonpressinpushbutton4.
functionpushbutton4_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton4(seeGCBO)
a=get(handles.pushbutton4,'
%---Executesonbuttonpressinpushbutton5.
functionpushbutton5_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton5(seeGCBO)
a=get(handles.pushbutton5,'
%---Executesonbuttonpressinpushbutton6.
functionpushbutton6_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton6(seeGCBO)
a=get(handles.pushbutton6,'
%---Executesonbuttonpressinpushbutton7.
functionpushbutton7_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton7(seeGCBO)
a=get(handles.pushbutton7,'
locaval=strcat(locaval,a)
%---Executesonbuttonpressinpushbutton8.
functionpushbutton8_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton8(seeGCBO)
a=get(handles.pushbutton8,'
%---Executesonbuttonpressinpushbutton9.
functionpushbutton9_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton9(seeGCBO)
a=get(handles.pushbutton9,'
%---Executesonbuttonpressinpushbutton10.
%hObjecthandletopushbut