




免费预览已结束,剩余9页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
设计说明书题 目: 基于MATLAB GUI计算器的设计与实现 姓 名: 刘文斌 学 号: 0904705051 指 导 教 师: 俞学兰 专 业 年 级: 机械设计及其自动化(机械电子工程方向) 09机电2班 所在学院和系: 机械工程学院 完 成 日 期: 2012年8月1日 1 绪论用户界面(或接口)是指:人与机器(或程序)之间交互作用的工具和方法。如键盘、鼠标、跟踪球、话筒都可成为与计算机交换信息的接口。图形用户界面(Graphical User Interfaces ,GUI)则是由窗口、光标、按键、菜单、文字说明等对象(Objects)构成的一个用户界面。用户通过一定的方法(如鼠标或键盘)选择、激活这些图形对象,使计算机产生某种动作或变化,比如实现计算、绘图等。2 GUI开发环境 GUI开发环境(GUI Development Environment, GUIDE)是MATLAB提供了一套可视化的创建图形窗口的工具,使用用户界面开发环境可方便的创建GUI应用程序, 它可以根据用户设计的GUI布局,自动生成M文件的框架,用户使用这一框架编制自己的应用程序。GUI开发环境界面如下图所示:3.设计目的运用MATLAB实现GUI的用户界面及程序设计。4 题目分析 4.1程序设计的基本要求l 熟悉和掌握MATLAB程序设计方法l 掌MATLAB GUI程序设计 4.2设计内容 要求利用MATLAB GUI设计实现一个图形用户界面的简易计算器程序,要求实现:具有良好的用户图形界面,实现十进制的加、减、乘、除、乘方等简易计算。科学计算函数,包含开方、三角函数运算功能有清除键和退格键以及基本的09数字键和小数点 4.3题目分析 本题目通过MATLAB的GUI程序设计,在GUI设计中主要用到三种控件,显示框用到文本编辑框(edit text), 说明框用到静态文本框(static text),数字以及运算等按钮用到命令按钮(push button).然后在通过各个按钮的回调函数,实现简单的计算功能。4.4设计思路首先用MATLAB GUI功能,设置一个静态文本框和一个文本编辑框,以及命令按钮,调整好各控件的颜色、大小。通过双击各按钮来改写其属性,在M文件中编写其回调函数,最后再运行调试。5 GUI程序设计 5.1、设计图形界面设计步骤: (1) 在布局编辑器中布置控件,(2) 使用几何位置排列工具对控件的位置进行调整;(3) 设计控件的属性;GUI 环境下简易计算器用户界面如下图所示:5.2设置控件的标识 控件的标识(Tag)是对于各控件的识别,每个控件载创建时都会由开发环境自动产生一个标识,在程序设计中,为了编辑、记忆和维护的方便,一般为控件设置一个新的标识。5.3各功能模块实现 GUI图形界面的功能要通过一定的设计思路和计算方法由特定的程序来实现。为了实现程序的功能,还需要在运行程序前编写一些代码,完成程序中变量的赋值、输入输出、计算及绘图等工作。在创建 GUI 时系统已经为其自动生成了 M 文件,该文件中包含 GUI 中控件对应的响应函数,及系统函数等。部分具体函数代码及相关注释:l 数字键设计:09以及小数点都一样,只是参数不同:%全局变量locaval用于存储用户输入的多位数值%全局变量gloval2用于存储待处理的第二位数值function pushbutton0_Callback(hObject, eventdata, handles)global locaval;a = get(handles.pushbutton1,String);locaval=strcat(locaval,a);set(handles.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);l 运算符按钮处理“+、-、*、/ ”范例%全局变量flagnum存储运算符标志%全局变量global1用于储存第一个待处理数值function pushbutton_chu_Callback(hObject, eventdata, handles)a = get(handles.pushbutton10,String);b = get(handles.text1,String);set(handles.text1,String,a);global flagnumglobal gloval1global locavallocaval= ;flagnum=1;gloval1=b;guidata(hObject, handles);l 等号按钮运算实现%根据flagnum运算标志用switch决策语句实现相应计算%需注意相应数据类型的转化function pushbutton_dengyu_Callback(hObject, eventdata, handles)global flagnumglobal gloval1global gloval2global locavallocaval= ;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);switch flagnum; case 1 set(handles.text1,String,case1); case 2 set(handles.text1,String,case2); case 3 set(handles.text1,String,case3); case 4 set(handles.text1,String,case4);endguidata(hObject,handles)l Del按钮函数%算法实现:MATLAB是用矩阵存储数据的,相应的可以取文本框的前N-1实现其功能function pushbutton_BackSpace_Callback(hObject, eventdata, handles)textString = get(handles.text1,String);if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ;else ss=char(textString); l=length(textString); textString=ss(1:l-1);set(handles.text1,String,textString)endguidata(hObject,handles)l C清除按钮函数%把全局变量locaval清零function pushbutton_qinglin_Callback(hObject, eventdata, handles)global locavallocaval= ;set(handles.text1,String,0.);guidata(hObject,handles)6 结果分析6.1最终显示界面效果图:上述简易计算器用户界面,能完成简单的四则运算及四则混合运算功能。通过实际操练结果均与实际结果相吻合,能实现基本的功能。7 心得体会通过本次的MATLAB设计,让我对MATLAN尤其是GUI可视化图形界面的设计功能有了进一步的了解,认识到其功能的强大。在MATLAB简单计算器的设计中,了解了关于MATLAB图形用户界面的部分空间的使用方法,利用MATLAB的GUI提供的很多实用控件,方便用于设计自己的图形界面。1、通过MATLAB简单计算器的设计,初步了解了关于MATLAB图形用户界面的部分控件的使用方法。2、 MATLAB的GUI提供的很多实用的控件,方便用于设计属于自己的图形界面。3、 Matlab具有强大、丰富的内置函数和工具箱,界面设计时更加简洁、快捷与直观。参考书 目1李人厚、张平安.精通MATLAB综合辅导与指南M.西安:西安交通大学出版社.19992张智星.MATLAB程序设计与应用M.北京:清华大学出版设.2002.4.13施晓红 周佳。精通GUI图形界面教程M.北京:北京大学出版社,2003.4陈子为。基于Matlab GUI扫雷游戏的设计与实现J.现代电子技术,2008.附录仿真一个四则混合运算界面如下: 程序源代码function varargout = calculator(varargin)% 我的计算器,重要运用了文本框和按钮。gui_Singleton = 1;gui_State = struct(gui_Name, mfilename, . gui_Singleton, gui_Singleton, . gui_OpeningFcn, calculator_OpeningFcn, . gui_OutputFcn, calculator_OutputFcn, . gui_LayoutFcn, , . gui_Callback, );if nargin & ischar(varargin1) gui_State.gui_Callback = str2func(varargin1);endif nargout varargout1:nargout = gui_mainfcn(gui_State, varargin:);else gui_mainfcn(gui_State, varargin:);endfunction calculator_OpeningFcn(hObject, eventdata, handles, varargin)handles.current_str=;%此为存储当前的字符串。% Choose default command line output for calculatorhandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes calculator wait for user response (see UIRESUME)% uiwait(handles.figure1);% - Outputs from this function are returned to the command line.function varargout = calculator_OutputFcn(hObject, eventdata, handles) varargout1 = handles.output;% - Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)%连接当前的字符串和1,如果按了1键的话。handles.current_str=strcat(handles.current_str,1);%在输出当前的字符串set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,2);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,3);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in pushbutton4.function pushbutton4_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,4);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in pushbutton5.function pushbutton5_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,5);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in pushbutton6.function pushbutton6_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,6);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in pushbutton7.function pushbutton7_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,7);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in pushbutton8.function pushbutton8_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,8);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in pushbutton9.function pushbutton9_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,9);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in pushbutton10.function pushbutton10_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,0);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in pushbutton11.function pushbutton11_Callback(hObject, eventdata, handles)% hObject handle to pushbutton11 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% - 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)% - Executes on button press in pushbutton13.function pushbutton13_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)% - Executes on button press in pushbutton14.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)% - Executes on button press in pushbutton15.function pushbutton15_Callback(hObject, eventdata, handles)% hObject handle to pushbutton15 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)function edit1_Callback(hObject, eventdata, handles)% hObject handle to edit1 (see GCBO)% eventdata reserved - to be defined in a future 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.function edit1_CreateFcn(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.if ispc set(hObject,BackgroundColor,white);else set(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction edit2_Callback(hObject, eventdata, handles)% hObject handle to edit2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,String) returns contents of edit2 as text% str2double(get(hObject,String) returns contents of edit2 as a double% - Executes during object creation, after setting all properties.function edit2_CreateFcn(hObject, eventdata, handles)% hObject handle to edit2 (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.if ispc set(hObject,BackgroundColor,white);else set(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction add_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,+);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in plus.function plus_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,-);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in chen.function chen_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,*);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in chu.function chu_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,/);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% - Executes on button press in clo.function clo_Callback(hObject, eventdata, handles)close(gcf);function guining_Callback(hObject, eventdata, handles)%讲所有的字符串归零。handles.current_str = ;set(handles.edit1,String,);set(handles.edit2,String,0);guidata(hObject, handles);function xiao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,.);guidata(hObject, handles);%小数点function result_Callback(hObject, eventdata, handles)st = get(handles.edit1,String);%计算结果,并存放到第二个编辑框中。val = eval(st);s = num2str(val);set(handles.edit2,String,s);guidata(hObject, handles);function xiao_ButtonDownFcn(hObject, eventdata, handles)% - Executes on button press in dele.function dele_Callback(hObject, eventdata, handles)% hObject handle to dele (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)s1 = handles.current_str;handles.current_str = s1(1:length(s1)-1);set(handles.edit1,String,handles.current_str);guidata(hObject, handles);% -function menu1_Callback(hObject, eventdata, handles)% h
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026届山东省菏泽市王浩屯中学英语九年级第一学期期末检测试题含解析
- 2026届贵州省黔东南州剑河县化学九上期中学业质量监测试题含解析
- 河南省郑州市桐柏一中学2026届九上化学期中调研模拟试题含解析
- 大兴安岭市重点中学2026届九年级英语第一学期期末学业水平测试模拟试题含解析
- 2026届陕西省宝鸡市渭滨区九年级英语第一学期期末经典模拟试题含解析
- 2026届山西省临汾市襄汾县九上化学期中达标测试试题含解析
- 信托资金借贷合同范文6篇
- 离婚协议中关于共同财产分割及人寿保险权益保障协议
- 离婚协议书(涉及跨境财产分割与法律适用)
- 猪场租赁合同(含饲料供应与养殖技术支持)
- 四年级《书法》教案上册
- 四高人群的膳食营养理论考核试题及答案
- 上海市幼儿园幼小衔接活动指导意见(修订稿)
- 药物不良反应处理课件
- 8 《大卫科波菲尔》课件高二上学期语文大单元教学同步备课课件(统编版选择性必修上册)
- 知识产权制度的发展
- AQ6111-2023个体防护装备安全管理规范
- DL-T747-2010发电用煤机械采制样装置性能验收导则
- 婚姻家庭法学-形成性考核四-国开(HB)-参考资料
- 2024年上海市行政执法类公务员招聘笔试参考题库附带答案详解
- 当代媒介素养 课件全 高萍 第1-10讲 媒介素养范畴-媒介效果与审查制度
评论
0/150
提交评论