matlabGUI设计简易科学计算器.docx

上传人:b****0 文档编号:10046104 上传时间:2023-05-23 格式:DOCX 页数:34 大小:555.50KB
下载 相关 举报
matlabGUI设计简易科学计算器.docx_第1页
第1页 / 共34页
matlabGUI设计简易科学计算器.docx_第2页
第2页 / 共34页
matlabGUI设计简易科学计算器.docx_第3页
第3页 / 共34页
matlabGUI设计简易科学计算器.docx_第4页
第4页 / 共34页
matlabGUI设计简易科学计算器.docx_第5页
第5页 / 共34页
matlabGUI设计简易科学计算器.docx_第6页
第6页 / 共34页
matlabGUI设计简易科学计算器.docx_第7页
第7页 / 共34页
matlabGUI设计简易科学计算器.docx_第8页
第8页 / 共34页
matlabGUI设计简易科学计算器.docx_第9页
第9页 / 共34页
matlabGUI设计简易科学计算器.docx_第10页
第10页 / 共34页
matlabGUI设计简易科学计算器.docx_第11页
第11页 / 共34页
matlabGUI设计简易科学计算器.docx_第12页
第12页 / 共34页
matlabGUI设计简易科学计算器.docx_第13页
第13页 / 共34页
matlabGUI设计简易科学计算器.docx_第14页
第14页 / 共34页
matlabGUI设计简易科学计算器.docx_第15页
第15页 / 共34页
matlabGUI设计简易科学计算器.docx_第16页
第16页 / 共34页
matlabGUI设计简易科学计算器.docx_第17页
第17页 / 共34页
matlabGUI设计简易科学计算器.docx_第18页
第18页 / 共34页
matlabGUI设计简易科学计算器.docx_第19页
第19页 / 共34页
matlabGUI设计简易科学计算器.docx_第20页
第20页 / 共34页
亲,该文档总共34页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

matlabGUI设计简易科学计算器.docx

《matlabGUI设计简易科学计算器.docx》由会员分享,可在线阅读,更多相关《matlabGUI设计简易科学计算器.docx(34页珍藏版)》请在冰点文库上搜索。

matlabGUI设计简易科学计算器.docx

matlabGUI设计简易科学计算器

M

A

T

L

A

B

班级:

姓名:

学号:

计算器

Ø题目

本题目通过MATLAB的gui程序设计较为简单,在gui设计中主要用到三种控件,文本编辑框(edittext),静态文本框(Statictext),命令按钮(pushbutton)。

然后在通过各个按钮的回调函数,实现简单的计算功能。

Ø1、功能介绍

(1)具有友好的用户图形界面。

实现十进制数的加、减、乘、除、乘方、开方等简单计算。

(2)具有科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等函数运行。

(注:

三角函数计算的是弧度而不是角度)。

(3)有清除键,能清除操作。

Ø2、功能实现

程序由两个部分组成:

MATLAB代码(.m文件)和GUI图形(.fig)。

程序使用的流程:

直接利用图形界面中的按键键入所需数值、运算符等即可得出结果。

备注:

软件版本:

MATLAB2011b

首先用MATLABGUI功能,在绘制一个静态文本框和一个文本编辑框,以及33个命令按钮,调整好各控件大小、颜色,整体布局如图所示:

(附录中有相关属性修改介绍)

然后通过双击各个按钮来改写其属性,在m文件中编写其回调函数,最后在运行调试。

2.1各功能界面设计

GUI设计界面:

注:

底部边框用(Panel)工具添加,有两种设计顺序。

(1、先加底部边框,再在底部边框上画功能键。

2、先画功能键,布好局,画底框,全选功能键拖动到底框上。

2.2各功能模块实现(可根据需要增减功能键)

算法设计:

1.数字键设计:

0—9以及小数点函数都一样,只是参数不同:

例如:

按键‘1’响应:

globaljj

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;

2.四则运算函数:

‘+’功能响应:

textString=get(handles.text1,'String');

textString=strcat(textString,'+');

set(handles.text1,'String',textString)

‘-’功能响应:

textString=get(handles.text1,'String');

textString=strcat(textString,'-');

set(handles.text1,'String',textString)

‘×’功能响应:

textString=get(handles.text1,'String');

textString=strcat(textString,'*');

set(handles.text1,'String',textString)

‘÷’功能响应:

textString=get(handles.text1,'String');

textString=strcat(textString,'/');

set(handles.text1,'String',textString)

3.科学计算函数:

例如:

‘sin’功能响应:

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

4.退格键(DEL):

通过取屏幕值,计算出其字符长度,然后取其前N-1项的值来实现退格:

globaljj

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;

5.清屏键函数(AC):

set(handles.text1,'String','0.');

2.3各模块程序添加方法

选中一个需添加程序的功能键,右击,ViewCallbacks,Callback,出现如下图所示界面。

(红色框中为所需添加的程序)其他功能键添加方法类似。

2.4各模块实现结果

(1)数字键:

(2)四则运算函数:

(3)科学计算函数:

Cos0的计算结果:

arctan2的计算结果:

经过计算,这些结果均与实际结果相吻合,计算器的功能实现的较为完好。

Ø3、程序总结:

(1)小数点可以连续输入。

解决方法是:

用strfind函数查看文本框里有几个小数点,如果已经有一个了,再按小数点就保持不变。

(2)按过运算符号后一个数不等于一个数,比如:

输入1,按等号,会出来一个3,经过长时间分析得知,这是由于在按运算符号时,系统记录了文本框里的数但没有清空,才会出现这种问题。

解决方法是再申请一个不同于加减乘除的另一个符号,并将按过运算符后记录的数值置0。

Ø4、心得体会:

通过本次的MATLAB课程设计,让我对MATLAB尤其是其GUI设计的功能有了进一步的了解,认识到了它功能的强大。

在MATLAB简单计算器的设计中,了解了关于MATLAB图形用户界面的部分控件的使用方法;利用MATLAB的GUI提供的很多实用的控件,方便用于设计属于自己的图形界面和自己所需要实现的功能。

Ø5、附录:

(1)功能块属性修改表(双击功能块可进入)

(2)主要程序

functionvarargout=untitled(varargin)

%UNTITLEDM-untitled.fig

%UNTITLED,byitself,createsanewUNTITLEDorraisestheexisting

%singleton*.

%

%H=UNTITLEDreturnsthehandletoanewUNTITLEDorthehandleto

%theexistingsingleton*.

%

%UNTITLED('Property','Value',...)createsanewUNTITLEDusingthe

%givenpropertyvaluepairs.Unrecognizedpropertiesarepassedvia

%varargintountitled_OpeningFcn.Thiscallingsyntaxproducesa

%warningwhenthereisanexistingsingleton*.

%

%UNTITLED('CALLBACK')andUNTITLED('CALLBACK',hObject,...)callthe

%localfunctionnamedCALLBACKinUNTITLED.Mwiththegiveninput

%arguments.

%

%*SeeGUIOptionsonGUIDE'sToolsmenu.Choose"GUIallowsonlyone

%instancetorun(singleton)".

%

%Seealso:

GUIDE,GUIDATA,GUIHANDLES

%Edittheabovetexttomodifytheresponsetohelpuntitled

%LastModifiedbyGUIDEv2.519-Dec-201311:

25:

45

%Begininitializationcode-DONOTEDIT

gui_Singleton=1;

gui_State=struct('gui_Name',m,...

'gui_Singleton',gui_Singleton,...

'gui_OpeningFcn',@untitled_OpeningFcn,...

'gui_OutputFcn',@untitled_OutputFcn,...

'gui_LayoutFcn',[],...

'gui_Callback',[]);

ifnargin&&ischar(varargin{1})

gui_State.gui_Callback=str2func(varargin{1});

end

ifnargout

[varargout{1:

nargout}]=gui_mainfcn(gui_State,varargin{:

});

else

gui_mainfcn(gui_State,varargin{:

});

end

%Endinitializationcode-DONOTEDIT

%---Executesjustbeforeuntitledismadevisible.

functionuntitled_OpeningFcn(hObject,eventdata,handles,varargin)

%Thisfunctionhasnooutputargs,seeOutputFcn.

%hObjecthandletofigure

%eventdatareserved-tobedefinedinafutureversionofMATLAB

%handlesstructurewithhandlesanduserdata(seeGUIDATA)

%vararginunrecognizedPropertyName/PropertyValuepairsfromthe

%commandline(seeVARARGIN)

%Choosedefaultcommandlineoutputforuntitled

handles.output=hObject;

%Updatehandlesstructure

guidata(hObject,handles);

%UIWAITmakesuntitledwaitforuserresponse(seeUIRESUME)

%uiwait(handles.figure1);

globaljj;

set(handles.text1,'String','0.');

jj=0;

%---Outputsfromthisfunctionarereturnedtothecommandline.

functionvarargout=untitled_OutputFcn(hObject,eventdata,handles)

%varargoutcellarrayforreturningoutputargs(seeVARARGOUT);

%hObjecthandletofigure

%eventdatareserved-tobedefinedinafutureversionofMATLAB

%handlesstructurewithhandlesanduserdata(seeGUIDATA)

%Getdefaultcommandlineoutputfromhandlesstructure

varargout{1}=handles.output;

%---Executesonbuttonpressinpushbutton1.

functionpushbutton1_Callback(hObject,eventdata,handles)

%hObjecthandletopushbutton1(seeGCBO)

%eventdatareserved-tobedefinedinafutureversionofMATLAB

%handlesstructurewithhandlesanduserdata(seeGUIDATA)

globaljj

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.

functionpushbutton2_Callback(hObject,eventdata,handles)

%hObjecthandletopushbutton2(seeGCBO)

%eventdatareserved-tobedefinedinafutureversionofMATLAB

%handlesstructurewithhandlesanduserdata(seeGUIDATA)

globaljj

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.

functionpushbutton3_Callback(hObject,eventdata,handles)

%hObjecthandletopushbutton3(seeGCBO)

%eventdatareserved-tobedefinedinafutureversionofMATLAB

%handlesstructurewithhandlesanduserdata(seeGUIDATA)

globaljj

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;

%---Executesonbuttonpressinpushbutton4.

functionpushbutton4_Callback(hObject,eventdata,handles)

%hObjecthandletopushbutton4(seeGCBO)

%eventdatareserved-tobedefinedinafutureversionofMATLAB

%handlesstructurewithhandlesanduserdata(seeGUIDATA)

globaljj

textString=get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)

set(handles.text1,'String','4');

else

textString=strcat(textString,'4');

set(handles.text1,'String',textString)

end

jj=0;

%---Executesonbuttonpressinpushbutton5.

functionpushbutton5_Callback(hObject,eventdata,handles)

%hObjecthandletopushbutton5(seeGCBO)

%eventdatareserved-tobedefinedinafutureversionofMATLAB

%handlesstructurewithhandlesanduserdata(seeGUIDATA)

globaljj

textString=get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)

set(handles.text1,'String','5');

else

textString=strcat(textString,'5');

set(handles.text1,'String',textString)

end

jj=0;

%---Executesonbuttonpressinpushbutton6.

functionpushbutton6_Callback(hObject,eventdata,handles)

%hObjecthandletopushbutton6(seeGCBO)

%eventdatareserved-tobedefinedinafutureversionofMATLAB

%handlesstructurewithhandlesanduserdata(seeGUIDATA)

globaljj

textString=get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)

set(handles.text1,'String','6');

else

textString=strcat(textString,'6');

set(handles.text1,'String',textString)

end

jj=0;

%---Executesonbuttonpressinpushbutton7.

functionpushbutton7_Callback(hObject,eventdata,handles)

%hObjecthandletopushbutton7(seeGCBO)

%eventdatareserved-tobedefinedinafutureversionofMATLAB

%handlesstructurewithhandlesanduserdata(seeGUIDATA)

globaljj

textString=get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)

set(handles.text1,'String','7');

else

textString=strcat(textString,'7');

set(handles.text1,'String',textString)

end

jj=0;

%---Executesonbuttonpressinpushbutton8.

functionpushbutton8_Callback(hObject,eventdata,handles)

%hObjecthandletopushbutton8(seeGCBO)

%eventdatareserved-tobedefinedinafutureversionofMATLAB

%handlesstructurewithhandlesanduserdata(seeGUIDATA)

globaljj

textString=get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)

set(handles.text1,'String','8');

else

textString=strcat(textString,'8');

set(handles.text1,'String',textString)

end

jj=0;

%---Executesonbuttonpressinpushbutton9.

functionpushbutton9_Callback(hObject,eventdata,handles)

%hObjecthandletopushbutton9(seeGCBO)

%eventdatareserved-tobedefinedinafutureversionofMATLAB

%handlesstructurewithhandlesanduserdata(seeGUIDATA)

globaljj

textString=get(handles.text1,'String');

if(strcmp(textString,'0.')==1)&(jj==0)

set(handles.text1,'String','9');

else

textString=strcat(textString,'9');

set(handles.te

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2