matlab-GUI-设计简易科学计算器.docx

上传人:scccc 文档编号:13618765 上传时间:2022-01-20 格式:DOCX 页数:26 大小:436.84KB
返回 下载 相关 举报
matlab-GUI-设计简易科学计算器.docx_第1页
第1页 / 共26页
matlab-GUI-设计简易科学计算器.docx_第2页
第2页 / 共26页
matlab-GUI-设计简易科学计算器.docx_第3页
第3页 / 共26页
matlab-GUI-设计简易科学计算器.docx_第4页
第4页 / 共26页
matlab-GUI-设计简易科学计算器.docx_第5页
第5页 / 共26页
点击查看更多>>
资源描述

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

1、班级:姓名:M A TLAB大作业计算器?题目本题目通过MATLAB勺gui程序设计较为简单,在gui设计中主要用到三种 控件,文本编辑框(edit text ),静态文本框(Static text ),命令按钮(push button )。然后在通过各个按钮的回调函数,实现简单的计算功能。? 1、功能介绍(1)具有友好的用户图形界面。实现十进制数的加、减、乘、除、乘方、开 方等简单计算。(2)具有科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、 开方、指数等函数运行。(注:三角函数计算的是弧度而不是角度 )。(3)有清除键,能消除操作。? 2、功能实现程序由两个部分组成:M

2、ATLAB (.m文件)和GUI图形(.fig )。程序使用的流程:直接利用图形界面中的按键键入所需数值、运算符等即可 得出结果。备注:软件版本: MATLAB 2011b首先用MATLAB GU功能,在绘制一个静态文本框和一个文本编辑框,以及33个命令按钮,调整好各控件大小、颜色,整体布局如图所示:(附录中有相关属性修改介绍)然后通过双击各个按钮来改写其属性,在m文件中编写其回调函数,最后在运行 调试。1.1 各功能界面设计GUI设计界面:注:底部边框用(Panel)工具添加,有两种设计顺序。(1、先 加底部边框,再在底部边框上画功能键。2、先画功能键,布好局,画底框,全选功能键拖动到底框上

3、。)2.2各功能模块实现(可根据需要增减功能键)算法设计:1.数字键设计:09以及小数点函数都一样,只是参数不同:例如:按键1响应:global jjtextString = get(handles.text1, String);if (strcmp(textString, set(handles.text1,0. )=1)&(jj=0) String , 1);elsetextstring =strcat(textString, 1); set(handles.text1, String ,textString) end jj=0; 2.四则运算函数:+功能响应:textString = g

4、et(handles.text1, String); textString =strcat(textString, +);set(handles.text1, String ,textString)-功能响应:textString = get(handles.text1, String);textString =strcat(textString, -);set(handles.text1, String ,textString)x功能响应:textString = get(handles.text1, String);textString =strcat(textString, *);set

5、(handles.text1, String ,textString)功能响应:textString = get(handles.text1,String );textString =strcat(textString,/);set(handles.text1,String ,textString)3 .科学计算函数:例如:sin 功能响应:textString = get(handles.text1, String);0.尸=1)String ,0.);%f );String ,a)if (strcmp(textString, set(handles.text1, elsea = strre

6、ad(textString, a=sin(a);set(handles.text1, endN-1项的值来4 .退格键(DEL):通过取屏幕值,计算出其字符长度,然后取其前 实现退格:global jjtextString = get(handles.text1, String);if (strcmp(textString, set(handles.text1, else ss=char(textString); l=length(textString); textString=ss(1:l-1);0. )=1)&(jj=0)String ,0.);String ,textString)set

7、(handles.text1, end jj=0;5 .清屏键函数(AC): set(handles.text1, String , 0.);2.3各模块程序添加方法选中一个需添加程序的功能键,右击, View Callbacks , Callback ,出现如下图 所示界面。(红色框中为所需添加的程序)其他功能键添加方法类似。untkUrrEditor -(1)数字键:(2)四则运算函数:(3)科学计算函数:Cos0的计算结果:arctan2的计算结果:经过计算,这些结果均与实际结果相吻合,计算器的功能实现的较为完好。? 3、程序总结:(1)小数点可以连续输入。解决方法是:用 strfind

8、 函数查看文本框里有几个 小数点,如果已经有一个了,再按小数点就保持不变。(2)按过运算符号后一个数不等于一个数,比如:输入 1,按等号,会出来一 个3,经过长时间分析得知,这是由于在按运算符号时,系统记录了文本框里的 数但没有清空,才会出现这种问题。解决方法是再中请一个不同于加减乘除的另 一个符号,并将按过运算符后记录的数值置 0o? 4、心得体会:通过本次的MATLA既程设计,让我对MATLA既其是其GUI设计的功能有了 进一步的了解,认识到了它功能的强大。在 MATLA前单计算器的设计中,了解 了关于MATLAB0形用户界面的部分控件的使用方法; 利用MATLAB勺GUI提供的 很多实用

9、的控件,方便用于设计属于自己的图形界面和自己所需要实现的功能。? 5、附录:(1)功能块属性修改表(双击功能块可进入)(2)主要程序function varargout = untitled(varargin)%UNTITLED M- untitled.fig% UNTITLED, by itself, creates a new UNTITLEDor raises the existing% singleton*.% H = UNTITLED returns the handle to a new UNTITLED or the handle to% the existing singlet

10、on*.% UNTITLED(Property,Value,.)creates a newUNTITLEDusing the% given property value pairs. Unrecognized properties are passed via % varargin to untitled_OpeningFcn. This calling syntax produces a % warning when there is an existing singleton*.% UNTITLED(CALLBACK) and UNTITLED(CALLBACK,hObject,.) ca

11、ll the% local function named CALLBACK in UNTITLED.M with the given input% arguments.% *See GUI Options on GUIDEs 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 untitled% Last Modified by

12、GUIDE v2.5 19-Dec-2013 11:25:45% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct( gui_Name, m, .gui_Singleton , gui_Singleton, .gui_OpeningFcn , untitled_OpeningFcn, gui_OutputFcn , untitled_OutputFcn, gui_LayoutFcn , ,.gui_Callback , );if nargin & ischar(varargin1)gui_St

13、ate.gui_Callback = str2func(varargin1);end if nargoutvarargout1:nargout = gui_mainfcn(gui_State, varargin:);elsegui_mainfcn(gui_State, varargin:);end% End initialization code - DO NOT EDIT% - Executes just before untitled is made visible.function untitled_OpeningFcn(hObject, eventdata, handles, vara

14、rgin)% 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 unrecognized PropertyName/PropertyValue pairs from the% command line (see VARARGIN

15、)% Choose default command line output for untitledhandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes untitled wait for user response (see UIRESUME)% uiwait(handles.figure1);global jj ;set(handles.text1, String ,0.);jj=0;% - Outputs from this function are retu

16、rned to the command line.function varargout = untitled_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (s

17、ee GUIDATA)% Get default command line output from handles structure varargout1 = handles.output;% - Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of

18、MATLAB% handles structure with handles and user data (see GUIDATA) global jjtextString = get(handles.text1, String);if (strcmp(textString,0. )=1)&(jj=0)set(handles.text1,String , 1);elsetextString =strcat(textString, 1 );set(handles.text1, String ,textString)endjj=0;% - Executes on button press in p

19、ushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) global jjtextString = get(handles.text1, String);if (strcmp(te

20、xtString,0. )=1)&(jj=0)set(handles.text1,String , 2);elsetextString =strcat(textString, 2 );set(handles.text1, String ,textString)endjj=0;% - Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)% hObject handle to pushbutton3 (see GCBO)% eventdata reserv

21、ed - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) global jjtextString = get(handles.text1, String);if (strcmp(textString,0. )=1)&(jj=0)set(handles.text1,String , 3);elsetextString =strcat(textString, 3 );set(handles.text1, String ,textString

22、) endjj=0;% - 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) global jjtextString

23、 = get(handles.text1, String);if (strcmp(textString,0. )=1)&(jj=0)set(handles.text1,String , 4);elsetextString =strcat(textString, 4);set(handles.text1, String ,textString)endjj=0;% - Executes on button press in pushbutton5.function pushbutton5_Callback(hObject, eventdata, handles)% hObject handle t

24、o pushbutton5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) global jjtextString = get(handles.text1, String);if (strcmp(textString,0. )=1)&(jj=0)set(handles.text1,String , 5);elsetextString =strcat(textString,

25、 5);set(handles.text1, String ,textString)endjj=0;% - 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 use

26、r data (see GUIDATA) global jjtextString = get(handles.text1, String);if (strcmp(textString,0. )=1)&(jj=0)set(handles.text1,String , 6);elsetextString =strcat(textString, 6 );set(handles.text1, String ,textString)endjj=0;% - Executes on button press in pushbutton7.function pushbutton7_Callback(hObje

27、ct, 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 jjtextString = get(handles.text1, String);if (strcmp(textString,0. )=1)&(jj=0)set(handles.text1,Strin

28、g , 7);elsetextString =strcat(textString, 7 );set(handles.text1, String ,textString)endjj=0;% - 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 MATL

29、AB% handles structure with handles and user data (see GUIDATA) global jjtextString = get(handles.text1, String);if (strcmp(textString,0. )=1)&(jj=0)set(handles.text1,String , 8);elsetextString =strcat(textString, 8);set(handles.text1, String ,textString) end jj=0;% - Executes on button press in push

30、button9.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) global jjtextString = get(handles.text1, String);if (strcmp(textS

31、tring,0. )=1)&(jj=0)set(handles.text1,String , 9);elsetextString =strcat(textString, 9);set(handles.text1, String ,textString) end jj=0;% - Executes on button press in pushbutton10.function pushbutton10_Callback(hObject, eventdata, handles)% hObject handle to pushbutton10 (see GCBO)% eventdata reser

32、ved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) global jjtextString = get(handles.text1, String);if (strcmp(textString,0. )=1)&(jj=0)set(handles.text1,String , 0);elsetextString =strcat(textString, 0);set(handles.text1, String ,textString

33、) end jj=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 jjtextS

34、tring = get(handles.text1, String);if (strcmp(textString,0.尸=1)set(handles.text1,String , 0.);jj=1;elsetextString =strcat(textString, . );set(handles.text1, String ,textString) end% - Executes on button press in pushbutton13.function pushbutton13_Callback(hObject, eventdata, handles)% hObject handle

35、 to pushbutton13 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)textString = get(handles.text1, String);textString =strcat(textString, / );set(handles.text1, String ,textString)% - Executes on button press in pu

36、shbutton14.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)textString = get(handles.text1, String);if (strcmp(textString

37、,0. )=1)set(handles.text1,String,();elsetextString =strcat(textString, ( );set(handles.text1, String ,textString) end% - Executes on button press in pushbutton16.function pushbutton16_Callback(hObject, eventdata, handles)% hObject handle to pushbutton16 (see GCBO)% eventdata reserved - to be defined

38、 in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1, String);if (strcmp(textString,0.尸=1)set(handles.text1,String,);elsetextString =strcat(textString, ) );set(handles.text1, String ,textString)end% - Executes on button press in p

39、ushbutton17.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)textString = get(handles.text1, String);textString =strcat(t

40、extString, - );set(handles.text1, String ,textString)% - 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 a

41、nd user data (see GUIDATA) textString = get(handles.text1, String);% if(strcmp(textString,0.)=1)% set(handles.text1,String,0.);% else% pushbutton20 b c = strread(textString, %f %c %f);% clear ans;% switch b% case *%ans=pushbutton20*c;% case /%if (c=0)% ans=Error Divided by zero;%else%ans=pushbutton2

42、0/c;%end% case -%ans=pushbutton20-c;% case +% ans=pushbutton20+c;% endans =eval(textString);set(handles.text1, String ,ans)% - Executes on button press in pushbutton19.function pushbutton19_Callback(hObject, eventdata, handles)% hObject handle to pushbutton19 (see GCBO)% eventdata reserved - to be d

43、efined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)textString = get(handles.text1, String);textString =strcat(textString, +);set(handles.text1, String ,textString)% - Executes on button press in pushbutton20.function pushbutton20_Callback(hObject, eventda

44、ta, handles)% hObject handle to pushbutton20 (see GCBO)% eventdata reserved - to be defined in a future version of MATLABString );% handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,0.尸=1)String , 0.);%f );%strcmp(textString,0.) if (strcmp(textString, set(handles.text1, elsea = strread(textString, a=acot(a);set(handles.text1, String ,a) end% - Executes on button press in pushbutton21.function pushbutton21_Callback(

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

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1