1、1、设计目的运用MATLAB实现MATLAB的GUI程序设计。2、题目分析2.1课程设计的基本要求:A. 熟悉和掌握MATLAB 程序设计方法。B. 掌握MATLAB GUI程序设计。2.2课程设计的内容要求利用MATLAB GUI设计实现一个图形用户界面的计算器程序,要求实现:A. 具有友好的用户图形界面。实现十进制数的加、减、乘、除、乘方、取模等简单计算。B. 科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等函数运行。C.能够保存上次历史计算的答案,显示答案存储器中的内容。D. 有清除键,能清除操作。E. 独立存储器功能,使之可以直接输入存储器,可与存储器中的
2、数值相加减。能够清除独立存储器中的内容。2.3题目分析本题目通过MATLAB的GUI程序设计较为简单,在GUI设计中主要用到三种控件,显示框用到文本编辑框(edit text),说明框用到静态文本框(Static text),数字以及运算等按钮用到命令按钮(push button)。然后再通过各个按钮的回调函数,实现简单的计算功能。3、总体设计首先用MATLAB GUI功能,在绘制一个静态文本框和一个文本编辑框,以及32个命令按钮,调整好各控件大小、颜色,整体布局如图所示:然后通过双击个按钮来改写其属性,在m文件中编写其回调函数,最后在运行调试。4、具体设计4.1 各功能界面设计GUI设计界面
3、:4.2 各功能模块实现算法设计:A. 数字键设计:09以及小数点函数都一样,只是参数不同:globaljjtextString = 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;B. 四则运算函数:textString = get(handles.text1,String);textString =st
4、rcat(textString,+);set(handles.text1,String,textString)C. 科学计算函数:textString = get(handles.text1,String);if(strcmp(textString,0.)=1)set(handles.text1,String,0.) ;elsea = strread(textString,%f);a=sin(a);set(handles.text1,String,a)end或textString=handles.text1;textString=sin(str2num(get(handles.text1,St
5、ring)*pi/180);set(handles.text1,String,num2str(textString)D. 退格键:通过取屏幕值,计算出其字符长度,然后取其前N-1项的值来实现退格:globaljjtextString = get(handles.text1,String);if(strcmp(textString,0.)=1)&(jj=0)set(handles.text1,String,0.) ;elsess=char(textString);l=length(textString);textString=ss(1:l-1);set(handles.text1,String,
6、textString)endjj=0;E. 清屏键函数:set(handles.text1,String,0.) ;F右键函数:gtext(大家好;我是智能机器人-myname is seven);close(gcf);4.3 各模块实现结果A. 数字键:B. 四则运算函数:C. 科学计算函数:Sin45的计算结果=经过计算,这些结果均与实际结果相吻合,计算器的功能实现的较为完好。5.2问题和解决方法:a.小数点可以连续输入。解决方法是:用strfind函数查看文本框里有几个小数点,如果已经有一个了,再按小数点就保持不变。b.按过运算符号后一个数不等于一个数,比如:输入1,按等号,会出来一个3
7、,经过长时间分析得知,这是由于在按运算符号时,系统记录了文本框里的数但没有清空,才会出现这种问题。解决方法是再申请一个不同于加减乘除的另一个符号,并将按过运算符后记录的数值置0。c.按对数函数键时,负数也能运算,通过加入if判断语句来判断输入的值是否为负,若为负则输出error.6、心得体会通过本次的MATLAB课程设计,让我对MATLAB尤其是其GUI设计的功能有了进一步的了解,认识到了它功能的强大。在MATLAB简单计算器的设计中,了解了关于MATLAB图形用户界面的部分控件的使用方法;利用MATLAB的GUI提供的很多实用的控件,方便用于设计属于自己的图形界面。7、附录(源代码)func
8、tionvarargout = jisuanqi(varargin)% JISUANQI M-file for jisuanqi.fig% JISUANQI, by itself, creates a new JISUANQI or raises the existing% singleton*.% H = JISUANQI returns the handle to a new JISUANQI or the handle to% the existing singleton*.% JISUANQI(CALLBACK,hObject,eventData,handles,.) calls th
9、e local% function named CALLBACK in JISUANQI.M with the given input arguments.% JISUANQI(Property,Value,.) creates a new JISUANQI or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before jisuanqi_OpeningFunction gets called. An% unrecognized pro
10、perty name or invalid value makes property application% stop. All inputs are passed to jisuanqi_OpeningFcn via varargin.% *See GUI Options on GUIDEs Tools menu. Choose GUI allows only one% instance to run (singleton).% See also: GUIDE, GUIDATA, GUIHANDLES% Copyright 2002-2003 The MathWorks, Inc.% Ed
11、it the above text to modify the response to help jisuanqi% Last Modified by GUIDE v2.5 04-Dec-2012 17:06:43% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct(gui_Name, mfilename,.gui_Singleton, gui_Singleton,.gui_OpeningFcn, jisuanqi_OpeningFcn,.gui_OutputFcn, jisuanqi_Out
12、putFcn,.gui_LayoutFcn, ,.gui_Callback, );ifnargin & ischar(varargin1)gui_State.gui_Callback = str2func(varargin1);endifnargoutvarargout1:nargout = gui_mainfcn(gui_State, varargin:);elsegui_mainfcn(gui_State, varargin:);end% End initialization code - DO NOT EDIT% - Executes just before jisuanqi is ma
13、de visible.functionjisuanqi_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 li
14、ne arguments to jisuanqi (see VARARGIN)% Choose default command line output for jisuanqihandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes jisuanqi wait for user response (see UIRESUME)% uiwait(handles.figure1);%定义全局变量jj 用于数字的设定globaljj ;%设置句柄,用于将按键接收的值返回给主程序
15、set(handles.text1,String,0.);jj=0;% - Outputs from this function are returned to the command line.functionvarargout = jisuanqi_OutputFcn(hObject, eventdata, handles)% varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a fu
16、ture version of MATLAB% handles structure with handles and user data (see GUIDATA)% Get default command line output from handles structurevarargout1 = handles.output;functionedit1_Callback(hObject, eventdata, handles)% hObject handle to edit1 (see GCBO)% eventdata reserved - to be defined in a futur
17、e version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,String) returns contents of edit1 as text% str2double(get(hObject,String) returns contents of edit1 as a double% - Executes during object creation, after setting all properties.functionedit1_CreateFcn
18、(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.ifispc &
19、isequal(get(hObject,BackgroundColor), get(0,defaultUicontrolBackgroundColor)set(hObject,BackgroundColor,white);end% - Executes on button press in pushbutton1.functionpushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a f
20、uture version of MATLAB% handles structure with handles and user data (see GUIDATA)globaljjtextString = 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 pre
21、ss in pushbutton2.functionpushbutton2_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)globaljjtextString = get(handles.text1,String);if(strcmp(
22、textString,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.functionpushbutton3_Callback(hObject, eventdata, handles)% hObject handle to pushbutton3 (see GCBO)% eventdata reserved -
23、to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)globaljjtextString = 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)endjj=0;% - Ex
24、ecutes on button press in pushbutton13.functionpushbutton13_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)globaljjtextString = get(handles.t
25、ext1,String);if(strcmp(textString,0.)=1)&(jj=0)set(handles.text1,String,+) ;elsetextString = get(handles.text1,String);textString =strcat(textString,+);set(handles.text1,String,textString)endjj=0;% - Executes on button press in pushbutton14.functionpushbutton14_Callback(hObject, eventdata, handles)%
26、 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)globaljjtextString = get(handles.text1,String);if(strcmp(textString,0.)=1)&(jj=0)set(handles.text1,String,-) ;elsetextString = get(h
27、andles.text1,String);textString =strcat(textString,-);set(handles.text1,String,textString)endjj=0;% - Executes on button press in pushbutton21.functionpushbutton21_Callback(hObject, eventdata, handles)% hObject handle to pushbutton21 (see GCBO)% eventdata reserved - to be defined in a future version
28、 of MATLAB% handles structure with handles and user data (see GUIDATA)%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,
29、2)set(handles.text1,String,textString)% - Executes on button press in pushbutton22.functionpushbutton22_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 (se
30、e GUIDATA)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)% - Executes on button press in pushbutton23.functionpushbutton23_Callback(hObject, eventdata, handles)% hObject hand
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1