matlab GUI 设计(简易科学计算器)

合集下载

(word完整版)用MATLAB GUI设计简单计算器

(word完整版)用MATLAB GUI设计简单计算器

摘要基于MATLAB GUI的计算器设计是利用GUIDE创建图形用户界面进行计算器设计。

设计计算器时,主要是考虑到计算器的易用性、功能的常用程度进行计算器界面与功能的设计。

通过调整控件和文本的布局及颜色,使界面简单大方、布局合理,达到界面友好的效果。

计算器设计时主要利用到get和set两个函数进行各个控件属性值的传递和设置。

计算器实现的功能有:数字0~9和小数点的输入显示,平方、开平方和对数的输入显示。

进行四则运算、正弦计算、余弦计算、正切计算和余切计算,可以求阶乘、求百分数和求倒数。

可以进行括号及变量x与变量y的输入,结合坐标轴编辑框和曲线颜色编辑框实现函数的曲线绘制。

最后运行调试,实现基于MATLAB GUI的计算器的设计。

MATLAB GUI介绍MATLAB是美国MathWorks公司出品的商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境。

MATLAB是matrix和laboratory两个词的组合,意为矩阵工厂,主要面对科学计算、可视化以及交互式程设计的高科技计算环境。

MATLAB是将数值分析、矩阵计算、科学数据可视化以及非线性动态系统的建模和仿真等诸多强大功能集成在一个易于使用的视窗环境中,为科学研究、工程设计以及必须进行有效数值计算的众多科学领域提供了一种全面的解决方案,并在很大程度上摆脱了传统非交互式程序设计语言的编辑模式,特别是所附带的30多种面向不同领域的工具箱支持,使得它在许多科学领域中成为计算机辅助设计和分析、算法研究和应用开发的基本工具和首选平台。

MATLAB的图形用户界面(Graphical User Interface,简称GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。

与早期计算机使用的命令行界面相比,图形界面对于用户来说在视觉上更易于接受。

MATLAB的图形用户界(GUI)是由光标,窗口,菜单、文字说明等对象构成一个用户界面。

matlab中gui设计计算器原理及设计方案

matlab中gui设计计算器原理及设计方案

matlab中gui设计计算器原理及设计方案在MATLAB中设计GUI(图形用户界面)计算器的基本原理和设计方案如下:基本原理:MATLAB的GUI设计基于事件驱动模型。

这意味着当用户与界面交互(例如,点击按钮或输入文本)时,会触发一个或多个事件。

这些事件会调用相应的回调函数,执行相应的操作。

设计方案:1. 启动MATLAB并创建GUI:打开MATLAB。

使用guide命令启动GUI设计器。

2. 添加GUI组件:在GUI设计器中,添加需要的组件,如按钮(push button)、文本框(edit text)、标签(label)等。

3. 设置组件属性:为每个组件设置必要的属性,例如位置、大小、标签文本等。

4. 编写回调函数:双击每个组件,MATLAB会自动生成一个默认的回调函数。

根据需要修改这些函数,以实现所需的功能。

例如,对于按钮,当用户点击它时,可以编写代码来执行相应的计算。

5. 测试GUI:在设计过程中,经常测试GUI以确保其正常工作。

可以使用simulink中的模拟功能,或直接在MATLAB环境中测试。

6. 保存和运行:保存GUI文件。

在MATLAB命令窗口中输入文件名(不包括扩展名),然后按Enter运行GUI。

7. 调试和优化:根据测试结果,调整回调函数和组件属性,优化GUI的行为和外观。

8. 发布:一旦GUI满足所有需求,可以发布它以供其他人使用。

这通常涉及将GUI打包为一个可执行文件或应用程序包。

9. 维护和更新:随着软件的发展,可能需要定期维护和更新GUI。

这可能涉及添加新功能、修复错误或改进性能。

在整个过程中,熟悉MATLAB的GUI设计和编程技巧是非常重要的。

此外,由于GUI设计可能需要反复的测试和调整,因此耐心和细心也是必不可少的。

matlabGUI人机用户界面设计[6]

matlabGUI人机用户界面设计[6]
图7-16 放置图形顾客界面旳控件
图7-18 对齐工具对话框
在界面之中还需要创建菜单,创建菜单能够经过菜单编辑器完毕。单击工具栏上旳菜单编辑器按钮,能够打开菜单编辑器对话框,在对话框中单击创建新菜单按钮,则能够创建新旳菜单,设置菜单属性如图7-20所示。
以空白界面类型为例,单击“OK”按钮,这时MATLAB将开启GUIDE旳图形界面,如图所示。
图 具有空白界面旳GUIDE图形界面
在GUIDE界面旳左侧为MATLAB旳控件面板,控件面板包括了能够在画布上绘制旳图形控件: (Push Button)、单项选择按钮(Toggle Button)、单项选择框(Radio Button)、复选框(Checkbox)、文本框(Edit Text)、静态文本框(Static Text)、滚动条(Slider)、组别框(Frame)、列表框(Listbox)、下拉框(Popup Menu)和坐标轴(Axes)。
2.1 GUIDE工具入门
使用图形句柄创建GUI旳过程繁琐,而且在程序编写好之前,顾客图形界面是不可见旳。所觉得了便于创建图形顾客界面,MATLAB提供了一种开发环境,能够帮助顾客创建图形顾客界面,这就是GUIDE——Graphic User Interface Development Environment。 在MATLAB中开启GUIDE旳措施: >>guide或者经过“Start”菜单项选择择“MATLAB”下旳“GUIDE”命令。
这时,将直接开启GUIDE Quick Start窗体,在这个窗体中,能够初步选择图形顾客界面旳类型,如图所示。
图 GUIDE旳迅速开启界面
在迅速开启界面中,能够选择四种类型旳新建界面:* 空白界面(Black GUI)。* 具有图形控件旳界面(GUI With Uicontrols)。* 具有菜单和坐标轴旳界面(GUI With Axes and Menu)。* 模式对话框(Modal Question Dialog)。

matlab-GUI-设计(简易科学计算器)

matlab-GUI-设计(简易科学计算器)

MATLAB大作业班级:姓名:学号:计算器➢题目本题目通过MATLAB的gui程序设计较为简单,在gui设计中主要用到三种控件,文本编辑框(edit text),静态文本框(Static text),命令按钮(push button)。

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

➢1、功能介绍(1)具有友好的用户图形界面。

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

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

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

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

➢2、功能实现程序由两个部分组成:MATLAB代码(.m文件)和GUI图形(.fig)。

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

备注:软件版本:MATLAB 2011b首先用MATLAB GUI功能,在绘制一个静态文本框和一个文本编辑框,以及33个命令按钮,调整好各控件大小、颜色,整体布局如图所示:(附录中有相关属性修改介绍)然后通过双击各个按钮来改写其属性,在m文件中编写其回调函数,最后在运行调试。

2.1 各功能界面设计GUI设计界面:注:底部边框用(Panel)工具添加,有两种设计顺序。

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

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

)2.2 各功能模块实现(可根据需要增减功能键)算法设计:1. 数字键设计:0—9以及小数点函数都一样,只是参数不同:例如:按键‘1’响应: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;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.') ;elsea = strread(textString, '%f');a=sin(a);set(handles.text1,'String',a)end4. 退格键(DEL):通过取屏幕值,计算出其字符长度,然后取其前N-1项的值来实现退格:global jjtextString = 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',textString)endjj=0;5. 清屏键函数(AC):set(handles.text1,'String','0.') ;2.3 各模块程序添加方法选中一个需添加程序的功能键,右击,View Callbacks,Callback,出现如下图所示界面。

Matlab中的GUI设计技巧和实现方法

Matlab中的GUI设计技巧和实现方法

Matlab中的GUI设计技巧和实现方法一、引言Matlab作为一款功能强大的科学计算软件,其GUI设计技巧和实现方法十分重要。

本文将介绍一些常用的GUI设计技巧和实现方法,并结合实际案例进行分析和讲解,旨在为读者提供一些参考和帮助。

二、Matlab中的GUI设计基础在开始GUI设计之前,需要先了解Matlab中的一些基础知识和概念。

Matlab 提供了一种称为GUIDE(Graphical User Interface Development Environment)的工具,可以帮助用户快速创建GUI界面。

通过GUIDE,用户可以方便地添加各种组件,如按钮、文本框、下拉列表等,并为这些组件添加相应的回调函数。

三、GUI界面的布局和美化1. 布局在设计GUI界面时,布局是一个很重要的方面。

合理的布局可以提高用户的使用体验。

Matlab中的GUI界面布局可以通过使用容器组件来实现,如面板(uipanel)和网格布局管理器(gridbaglayoutmanager)。

面板可以将相关的组件放在一起,网格布局管理器可以帮助用户实现自动布局。

2. 美化为了让GUI界面更加美观和易于使用,可以通过一些美化技巧来改善界面的外观。

例如可以使用颜色、字体和图标等元素来增加界面的可读性和吸引力。

此外,还可以使用一些动画效果来增强用户体验。

四、用户交互和数据处理1. 用户交互GUI界面的设计目的是提供给用户与程序进行交互的方式,因此用户交互十分重要。

可以通过一些交互方式来实现用户的操作,如按钮或菜单等。

同时,还可以使用一些交互控件,如滑块、复选框和下拉列表等,来帮助用户进行选择和输入。

2. 数据处理用户在GUI界面中输入的数据通常需要进行处理。

Matlab提供了丰富的数学和数据处理函数来处理各种数据。

通过编写相应的回调函数,可以实现对输入数据的处理和计算,并将结果显示在界面上。

五、实例分析:基于Matlab的图像处理应用为了更好地理解和应用GUI设计技巧和实现方法,我们以基于Matlab的图像处理应用为例进行实例分析。

matlab课程设计

matlab课程设计

m a t l a b课程设计(简单计算器的设计)(共16页)--本页仅作为文档封面,使用时请直接删除即可----内页可以根据需求调整合适字体及大小--matlab课程设计报告题目简易计算器的设计学院电子信息工程学院专业电子信息学生姓名和学号指导教师一、选题目的及意义GUI的广泛应用是当今计算机发展的重大成就之一,它极大地方便了非专业用户的使用。

人们从此不再需要死记硬背大量的命令,取而代之的是可以通过窗口、菜单、按键等方式来方便地进行操作,而在matlab有很简单的gui设计工具,我们可以通过这个工具轻松地构建我们想要的程序,从而实现与用户的信息交互。

本次课程设计是使用了matlab中的guide生成了简单的计算器程序。

二、源代码function varargout = Calculator(varargin)%Simple Calculator%@Anhui University% Begin initialization code - DO NOT EDITgui_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(varargin{1})= str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before Calculator is made visible.function Calculator_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 line arguments to Calculator (see VARARGIN)% Choose default command line output for Calculator= hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes Calculator wait for user response (see UIRESUME)% uiwait;% --- Outputs from this function are returned to the command line. function varargout = Calculator_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 (see GUIDATA)% Get default command line output from handles structure varargout{1} = ;% --- Executes on button press in p1.function p1_Callback(hObject, eventdata, handles)% hObject handle to p1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'1');set,'string',textstring)% --- Executes on button press in p2.function p2_Callback(hObject, eventdata, handles)% hObject handle to p2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'2');set,'string',textstring)% --- Executes on button press in p3.function p3_Callback(hObject, eventdata, handles)% hObject handle to p3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'3');set,'string',textstring)% --- Executes on button press in p4.function p4_Callback(hObject, eventdata, handles)% hObject handle to p4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'4');set,'string',textstring)% --- Executes on button press in p5.function p5_Callback(hObject, eventdata, handles)% hObject handle to p5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'5');set,'string',textstring)% --- Executes on button press in p6.function p6_Callback(hObject, eventdata, handles)% hObject handle to p6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'6');set,'string',textstring)% --- Executes on button press in p7.function p7_Callback(hObject, eventdata, handles)% hObject handle to p7 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'7');set,'string',textstring)% --- Executes on button press in p8.function p8_Callback(hObject, eventdata, handles)% hObject handle to p8 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'8');set,'string',textstring)% --- Executes on button press in p9.function p9_Callback(hObject, eventdata, handles)% hObject handle to p9 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'9');set,'string',textstring)% --- Executes on button press in add.function add_Callback(hObject, eventdata, handles)% hObject handle to add (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'+');set,'string',textstring)% --- Executes on button press in p0.function p0_Callback(hObject, eventdata, handles)% hObject handle to p0 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'0');set,'string',textstring)% --- Executes on button press in sub.function sub_Callback(hObject, eventdata, handles)% hObject handle to sub (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'-');set,'string',textstring)% --- Executes on button press in div.function div_Callback(hObject, eventdata, handles)% hObject handle to div (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'/');set,'string',textstring)% --- Executes on button press in mul.function mul_Callback(hObject, eventdata, handles)% hObject handle to mul (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'*');set,'string',textstring)% --- Executes on button press in denghao.function denghao_Callback(hObject, eventdata, handles)% hObject handle to denghao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=eval(textstring);set,'string',textstring)% --- Executes on button press in clear.function clear_Callback(hObject, eventdata, handles)% hObject handle to clear (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set,'string','0')% --------------------------------------------------------------------function exit_Callback(hObject, eventdata, handles)% hObject handle to exit (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) close(gcf)% --------------------------------------------------------------------function calculate_Callback(hObject, eventdata, handles)% hObject handle to calculate (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=eval(textstring);set,'string',textstring)% --- Executes on button press in point.function point_Callback(hObject, eventdata, handles)% hObject handle to point (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textstring=get,'string');textstring=strcat(textstring,'.');set,'string',textstring)设计功能:1.可进行加减乘除四则运算2.可进行清零,退出等三、界面设计及运行结果1-1. GUIDE 设计界面1-2.进入程序初始状态1-3.简单的实例测试五、心得及体会通过这次matlab课程设计,让我对matlab有了更深的了解。

matlab设计gui程序例题

matlab设计gui程序例题

近年来,MATLAB在科学计算和工程领域中的应用变得越来越广泛。

其中,GUI(Graphical User Interface)程序设计作为MATLAB的一个重要应用领域,对于帮助用户更直观地理解和使用MATLAB具有重要意义。

本文将以MATLAB设计GUI程序为主题,探讨其原理、实现方法和实际例题,并对其进行深入解析和探讨。

1. GUI程序设计的基本原理在MATLAB中,GUI程序设计主要通过图形用户界面工具包(GUIDE)来实现。

GUIDE工具包提供了一系列可以直观拖放的元素,包括按钮、文本框、下拉菜单等,用户可以通过拖拉这些元素的方式来设计出自己理想的界面。

MATLAB还提供了丰富的回调函数,用户可以将不同控件的回调函数与自定义函数进行绑定,实现交互式的操作。

2. GUI程序设计的实现方法为了更深入地理解GUI程序设计,我们可以以一个简单的例子来说明其实现方法。

假设我们需要设计一个简单的温度转换器,用户可以输入摄氏温度,然后通过点击按钮来实现摄氏温度到华氏温度的转换。

我们可以通过GUIDE工具包来设计界面,添加一个文本框用于输入摄氏温度,一个按钮用于触发转换操作,以及一个用于显示结果的文本框。

我们可以在回调函数中编写转换的过程,当用户点击按钮时,根据用户输入的摄氏温度进行计算,并将结果显示在结果文本框中。

3. 实际例题:温度转换器GUI程序设计现在,让我们按照上面的思路来实际设计一个温度转换器的GUI程序。

我们打开MATLAB并新建一个GUI程序,接着使用GUIDE工具包来设计界面,按照前文描述的方法添加文本框、按钮和结果文本框。

我们为按钮添加点击事件的回调函数,编写摄氏温度到华氏温度的转换算法,并将结果显示在结果文本框中。

我们保存并运行程序,测试其功能和效果。

4. 总结与思考通过上述例题的实际操作,我们更深入地理解了MATLAB的GUI程序设计原理和实现方法。

GUI程序设计能够帮助我们更直观地操作MATLAB,提高使用效率和便利性。

matlab简易计算器12页

matlab简易计算器12页

电子信息工程系实验报告课程名称:MATLAB语言实验项目名称:MATLAB图形用户界面设计实验时间:2019-5-8 班级:姓名:学号:实验目的: 1、熟悉MATLAB的主要控件使用方法。

2、熟悉MATLAB的GUI设计流程。

实验环境:硬件配置( Pentium(R) 4 CPU 2.40GHz,1GB内存)操作系统(Windows XP)编程软件:MATLAB7.0实验内容与结果:使用MATLAB的GUI接口设计一个简单的计算器。

效果图:程序源码部分函数及相关注释:%0~9数字键及小数点按钮代码范例%全局变量locaval用于存储用户输入的多位数值%全局变量gloval2用于存储待处理的第二位数值function pushbutton1_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);%运算符按钮处理“+、-、*、\”范例%全局变量flagnum存储运算符标志%全局变量global1用于储存第一个待处理数值function pushbutton10_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);%取相反数按钮“+\-”代码%算法实现:用零减去文本框现在的值,再赋值给文本框function pushbutton14_Callback(hObject, eventdata, handles) 成绩:指导教师(签名):global locaval;locaval=str2num(locaval);locaval=0-locaval;locaval=num2str(locaval);set(handles.text1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% 等号按钮运算实现%根据flagnum运算标志用switch决策语句实现相应计算%需注意相应数据类型的转化function pushbutton17_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 1set(handles.text1,'String',case1);case 2set(handles.text1,'String',case2);case 3set(handles.text1,'String',case3);case 4set(handles.text1,'String',case4);endguidata(hObject,handles)% BackSpace按钮函数%算法实现:MA TLAB是用矩阵存储数据的,相应的可以取文本框的前N-1实现其功能function pushbutton19_Callback(hObject, eventdata, handles)textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsess=char(textString);l=length(textString);textString=ss(1:l-1);set(handles.text1,'String',textString)endguidata(hObject,handles)%C清除按钮函数%把全局变量locaval清零function pushbutton20_Callback(hObject, eventdata, handles)global locavallocaval=' ';set(handles.text1,'String','0.');guidata(hObject,handles)%开平方函数function pushbutton22_Callback(hObject, eventdata, handles)textString = get(handles.text1,'String');textString=str2num(textString);textString=sqrt(textString);textString=num2str(textString);set(handles.text1,'String',textString);locaval=' ';guidata(hObject,handles)%取1/x函数function pushbutton24_Callback(hObject, eventdata, handles)global locavallocaval=str2num(locaval);locaval=1/locaval;set(handles.text1,'String',locaval);locaval=' ';guidata(hObject,handles)实验心得:!、通过MATLAB简单计算器的设计,初步了解了关于MATLAB图形用户界面的部分控件的使用方法。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

MATLAB大作业班级:姓名:学号:计算器➢题目本题目通过MATLAB的gui程序设计较为简单,在gui设计中主要用到三种控件,文本编辑框(edit text),静态文本框(Static text),命令按钮(push button)。

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

➢1、功能介绍(1)具有友好的用户图形界面。

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

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

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

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

➢2、功能实现程序由两个部分组成:MATLAB代码(.m文件)和GUI图形(.fig)。

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

备注:软件版本:MATLAB 2011b首先用MATLAB GUI功能,在绘制一个静态文本框和一个文本编辑框,以及33个命令按钮,调整好各控件大小、颜色,整体布局如图所示:(附录中有相关属性修改介绍)然后通过双击各个按钮来改写其属性,在m文件中编写其回调函数,最后在运行调试。

2.1 各功能界面设计GUI设计界面:注:底部边框用(Panel)工具添加,有两种设计顺序。

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

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

)2.2 各功能模块实现(可根据需要增减功能键)算法设计:1. 数字键设计:0—9以及小数点函数都一样,只是参数不同:例如:按键‘1’响应: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;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.') ;elsea = strread(textString, '%f');a=sin(a);set(handles.text1,'String',a)end4. 退格键(DEL):通过取屏幕值,计算出其字符长度,然后取其前N-1项的值来实现退格:global jjtextString = 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',textString)endjj=0;5. 清屏键函数(AC):set(handles.text1,'String','0.') ;2.3 各模块程序添加方法选中一个需添加程序的功能键,右击,View Callbacks,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)主要程序function varargout = untitled(varargin)%UNTITLED M-file for untitled.fig% UNTITLED, by itself, creates a new UNTITLED or raises the existing % singleton*.%% H = UNTITLED returns the handle to a new UNTITLED or the handle to% the existing singleton*.%% UNTITLED('Property','Value',...) creates a new UNTITLED using 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,...) call the% local function named CALLBACK in UNTITLED.M with the given input % arguments.%% *See GUI Options on GUIDE's 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 GUIDE v2.5 19-Dec-2013 11:25:45% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @untitled_OpeningFcn, ...'gui_OutputFcn', @untitled_OutputFcn, ...'gui_LayoutFcn', [], ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1: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, 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 unrecognized PropertyName/PropertyValue pairs from the % command line (see VARARGIN)% 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 returned 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 (see GUIDATA)% Get default command line output from handles structurevarargout{1} = 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 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 pushbutton2.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(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.function pushbutton3_Callback(hObject, eventdata, handles)% hObject handle to pushbutton3 (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','3') ;elsetextString =strcat(textString,'3');set(handles.text1,'String',textString)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 = 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 to 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,'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 user 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(hObject, 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,'String','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 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','8') ;elsetextString =strcat(textString,'8');set(handles.text1,'String',textString)endjj=0;% --- Executes on button press in pushbutton9.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(textString,'0.')==1)&(jj==0)set(handles.text1,'String','9') ;elsetextString =strcat(textString,'9');set(handles.text1,'String',textString)endjj=0;% --- Executes on button press in pushbutton10.function pushbutton10_Callback(hObject, eventdata, handles)% hObject handle to pushbutton10 (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','0') ;elsetextString =strcat(textString,'0');set(handles.text1,'String',textString)endjj=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 jjtextString = 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 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 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) 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 pushbutton16.function pushbutton16_Callback(hObject, eventdata, handles)% hObject handle to pushbutton16 (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,'0.')==1)set(handles.text1,'String',')') ;elsetextString =strcat(textString,')');set(handles.text1,'String',textString)end% --- Executes on button press in pushbutton17.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(textString,'-');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 and 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=pushbutton20/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 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 pushbutton20.function pushbutton20_Callback(hObject, eventdata, handles)% hObject handle to pushbutton20 (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');%strcmp(textString,'0.')if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=acot(a);set(handles.text1,'String',a)end% --- Executes on button press in pushbutton21.function pushbutton21_Callback(hObject, eventdata, handles)% hObject handle to pushbutton21 (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');%strcmp(textString,'0.')if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=sin(a);set(handles.text1,'String',a)end% --- Executes on button press in pushbutton22.function pushbutton22_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 (see GUIDATA) textString = get(handles.text1,'String');%strcmp(textString,'0.')if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=cos(a);set(handles.text1,'String',a)end% --- Executes on button press in pushbutton23.function pushbutton23_Callback(hObject, eventdata, handles)% hObject handle to pushbutton23 (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');%strcmp(textString,'0.')if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=tan(a);set(handles.text1,'String',a)end% --- Executes on button press in pushbutton24.function pushbutton24_Callback(hObject, eventdata, handles)% hObject handle to pushbutton24 (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');%strcmp(textString,'0.')if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=log2(a);set(handles.text1,'String',a)end% --- Executes on button press in pushbutton26.function pushbutton26_Callback(hObject, eventdata, handles)% hObject handle to pushbutton26 (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');%strcmp(textString,'0.')if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=log10(a);set(handles.text1,'String',a)end% --- Executes on button press in pushbutton27.function pushbutton27_Callback(hObject, eventdata, handles)% hObject handle to pushbutton27 (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','0.') ;elsess=char(textString);l=length(textString);textString=ss(1:l-1);set(handles.text1,'String',textString)endjj=0;% --- Executes on button press in pushbutton28.function pushbutton28_Callback(hObject, eventdata, handles)% hObject handle to pushbutton28 (see GCBO)% eventdata reserved - to 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 press in pushbutton29.function pushbutton29_Callback(hObject, eventdata, handles)% hObject handle to pushbutton29 (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,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=asin(a);set(handles.text1,'String',a)end% --- Executes on button press in pushbutton30.function pushbutton30_Callback(hObject, eventdata, handles)% hObject handle to pushbutton30 (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');%strcmp(textString,'0.')if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=cot(a);set(handles.text1,'String',a)end% --- Executes on button press in pushbutton31.function pushbutton31_Callback(hObject, eventdata, handles)% hObject handle to pushbutton31 (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');%strcmp(textString,'0.')if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=a*a;set(handles.text1,'String',a)end% --- Executes on button press in pushbutton32.function pushbutton32_Callback(hObject, eventdata, handles)% hObject handle to pushbutton32 (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');%strcmp(textString,'0.')if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=acos(a);set(handles.text1,'String',a)end% --- Executes on button press in pushbutton33.function pushbutton33_Callback(hObject, eventdata, handles)% hObject handle to pushbutton33 (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');%strcmp(textString,'0.')if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=atan(a);set(handles.text1,'String',a)end% --- Executes on button press in pushbutton34.function pushbutton34_Callback(hObject, eventdata, handles)% hObject handle to pushbutton34 (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');%strcmp(textString,'0.')if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=0-a;set(handles.text1,'String',a)end% --- 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) textString = get(handles.text1,'String');textString =strcat(textString,'*');set(handles.text1,'String',textString)% --- Executes on button press in pushbutton35.function pushbutton35_Callback(hObject, eventdata, handles)% hObject handle to pushbutton35 (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');%strcmp(textString,'0.')if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=sqrt(a);set(handles.text1,'String',a)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 && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end。

相关文档
最新文档