2020-03-03 13:28:42 来源:范文大全收藏下载本文
1、设计目的
运用MATLAB实现MATLAB的GUI程序设计。
2、题目分析
2.1课程设计的基本要求:
A.熟悉和掌握MATLAB 程序设计方法。 B.掌握MATLAB GUI程序设计。 2.2课程设计的内容
要求利用MATLAB GUI设计实现一个图形用户界面的计算器程序,要求实现: A.具有友好的用户图形界面。实现十进制数的加、减、乘、除、乘方、取模等简单计算。
B.科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等函数运行。
C.能够保存上次历史计算的答案,显示答案存储器中的内容。 D.有清除键,能清除操作。
E.独立存储器功能,使之可以直接输入存储器,可与存储器中的数值相加减。能够清除独立存储器中的内容。 2.3题目分析
本题目通过MATLAB的GUI程序设计较为简单,在GUI设计中主要用到三种控件,显示框用到文本编辑框(edit text),说明框用到静态文本框(Static text),数字以及运算等按钮用到命令按钮(push button)。然后再通过各个按钮的回调函数,实现简单的计算功能。
3、总体设计
首先用MATLAB GUI功能,在绘制一个静态文本框和一个文本编辑框,以及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
=char(textString); l=length(textString); textString=(1:l-1); set(handles.text1,\'String\',textString) end jj=0; E.清屏键函数:
set(handles.text1,\'String\',\'0.\') ; F.右键函数:
gtext(\'大家好;我是智能机器人-my name is seven\'); 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) % 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 the 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 property name or invalid value makes property application % stop.All inputs are paed to jisuanqi_OpeningFcn via varargin.%
% *See GUI Options on GUIDE\'s Tools menu.Choose \"GUI allows only one % instance to run (singleton)\".%
% See also: GUIDE, GUIDATA, GUIHANDLES % Copyright 2002-2003 The MathWorks, Inc.
% Edit the above text to modify the response to help jisuanqi % Last Modified by GUIDE v2.5 04-Dec-2012 17:06:43 % Begin initialization codeDO NOT EDIT
% --- Executes just before jisuanqi is made visible.
function jisuanqi_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn.% hObject handle to figure
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles emptyto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) 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;
% --- Executes on button pre in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) 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;
% --- Executes on button pre in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles) % hObject handle to pushbutton13 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) 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;
% --- Executes on button pre in pushbutton21.
function pushbutton21_Callback(hObject, eventdata, handles) % hObject handle to pushbutton21 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see 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 pre in pushbutton23.
function pushbutton23_Callback(hObject, eventdata, handles) % hObject handle to pushbutton23 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj
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;
% --- Executes on button pre in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles) % hObject handle to pushbutton5 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj
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;
% --- Executes on button pre in pushbutton15.
function pushbutton15_Callback(hObject, eventdata, handles) % hObject handle to pushbutton15 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) 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;
% --- Executes on button pre in pushbutton24.
function pushbutton24_Callback(hObject, eventdata, handles) % hObject handle to pushbutton24 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,\'String\'); %strcmp(textString,\'0.\') textString=handles.text1;
textString=cos(str2num(get(handles.text1,\'String\'))*pi/180); set(handles.text1,\'String\',num2str(textString)) % --- Executes on button pre in pushbutton26.
function pushbutton26_Callback(hObject, eventdata, handles) % hObject handle to pushbutton26 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj
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;
% --- Executes on button pre in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles) % hObject handle to pushbutton8 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj
textString = get(handles.text1,\'String\');
if(strcmp(textString,\'0.\')==1)&(jj==0) set(handles.text1,\'String\',\'9\') ; else
textString =strcat(textString,\'9\'); set(handles.text1,\'String\',textString) end jj=0;
% --- Executes on button pre in pushbutton17.
function pushbutton17_Callback(hObject, eventdata, handles) % hObject handle to pushbutton17 (see GCBO)
% eventdata reservedto 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,\'0.\')==1) set(handles.text1,\'String\',\')\') ; else
textString =strcat(textString,\')\'); set(handles.text1,\'String\',textString) end
% --- Executes on button pre in pushbutton27.
function pushbutton27_Callback(hObject, eventdata, handles) % hObject handle to pushbutton27 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,\'String\'); %strcmp(textString,\'0.\') textString=handles.text1;
textString=tan(str2num(get(handles.text1,\'String\'))*pi/180); set(handles.text1,\'String\',num2str(textString)) % --- Executes on button pre in pushbutton29.
function pushbutton29_Callback(hObject, eventdata, handles) % hObject handle to pushbutton29 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,\'String\'); if(strcmp(textString,\'0.\')==1)&(jj==0) set(handles.text1,\'String\',\'0.\') ; else
textString =strcat(textString,\'0\'); set(handles.text1,\'String\',textString) end jj=0;
% --- Executes on button pre in pushbutton11.
function pushbutton11_Callback(hObject, eventdata, handles) % hObject handle to pushbutton11 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.text1,\'String\',\'0.\') ;
% --- Executes on button pre in pushbutton19.
function pushbutton19_Callback(hObject, eventdata, handles) % hObject handle to pushbutton19 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString=get(handles.text1,\'String\') s=eval(textString)
set(handles.text1,\'String\',s)
% --- Executes on button pre in pushbutton30.
function pushbutton30_Callback(hObject, eventdata, handles) % hObject handle to pushbutton30 (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,\'String\'); %strcmp(textString,\'0.\') textString=handles.text1;
textString=cot(str2num(get(handles.text1,\'String\'))*pi/180); set(handles.text1,\'String\',num2str(textString)) % --- Executes on button pre in pushbutton32.
function pushbutton32_Callback(hObject, eventdata, handles) % hObject handle to pushbutton32 (see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %open(\'1.bmp\')
gtext(\'大家好;我是智能机器人-my name is seven\');
% ------ function J_Callback(hObject, eventdata, handles) % hObject handle to J (see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) close(gcf);
8、参考书目:
[1]《MATLAB语言及其在电子信息工程中的应用》 王洪元主编 清华大学出版社
[2] 《MATLAB中GUI的应用》 王洪元主编 清华大学出版社
人人范文网 m.inrrp.com.cn 手机版