基于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)是由光标,窗口,菜单、文字说明等对象构成一个用户界面。

简易计算器程序源代码

简易计算器程序源代码

简易计算器程序源代码下面是一个简易计算器程序的源代码,它可以执行基本的四则运算:```python#定义加法函数def add(x, y):return x + y#定义减法函数def subtract(x, y):return x - y#定义乘法函数def multiply(x, y):return x * y#定义除法函数def divide(x, y):if y == 0:return "除数不能为0"else:return x / y#显示菜单print("选择操作:")print("1. 相加")print("2. 相减")print("3. 相乘")print("4. 相除")#获取用户输入choice = input("输入你的选择(1/2/3/4): ")#获取用户输入的两个数字num1 = float(input("输入第一个数字: "))num2 = float(input("输入第二个数字: "))#根据用户选择执行相应操作if choice == '1':print(num1, "+", num2, "=", add(num1, num2))elif choice == '2':print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3':print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4':print(num1, "/", num2, "=", divide(num1, num2))else:print("请输入有效的选择")```运行这个程序,你将看到一个简易的计算器菜单。

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设计可能需要反复的测试和调整,因此耐心和细心也是必不可少的。

Matlab_-_GUI图形用户界面简单实用教程

Matlab_-_GUI图形用户界面简单实用教程
11
五 GUI图形用户界面
4. 界面激活 保存静态界面后,利用工具栏上的运行按钮激活该界面。 激活后可以看到两个同名文件:experiment1.m和 experiment1.fig。 experiment1.m文件在M文本编辑器中打开, experiment1.fig文件显示为操作界面,但是无法实现任何功 能,需要编写回调函数。
15
五 GUI图形用户界面
对本例来说,首先需要定义几个用户变量,以便记录参加 handles = 运算的数值,进行计算。 mainWnd : 132.0002 p2 : 24.0006
Operation : 149.0002 p1 handles, : 23.0006 function Experiment1_OpeningFcn(hObject, eventdata, varargin) pDot : 148.0002 text3 : 22.0006 handles.output = hObject; pLog10 : 147.0002 text2 : 21.0006 handles.s_Exp=‘’; % 以字符串的形式记录计算表达式 pLog : 146.0002 frame4 : 20.0006 handles.s_Num=‘’; %以字符串的形式记录编辑框中要显示的数值 pCos : 145.0002 text1 : 19.0006 handles.i=1; % s_Exp的计数器 pClear : 18.0006 pSin : 144.0002 pDel : 143.0002 handles.j=1; % s_Num的计数器 pDone : 17.0006 pExp : 16.0006 handles.flag=0; pAdd : 142.0002 %用来区分四则运算和其他运算的标志 pDiv : 141.0002 frame3 : 15.0007 % Update handles structure pMul : 140.0002 frame2 : 133.0002 guidata(hObject, handles); p0 : 139.0002 p_Clear : 151.0002 p9 : 138.0002 p_Done : 150.0002 p8 : 137.0002 output : 132.0002 p7 : 136.0002 s_Exp : '50' p6 : 135.0002 s_Num : '5' p5 : 134.0002 i :2 p4 : 26.0006 j :2 p3 : 25.0006 flag : 0

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课程设计

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简易计算器设计步骤与回调函数设计说明

matlab简易计算器设计步骤与回调函数设计说明

简易算术计算器的设计一、算术计算器的功能:能进行简单的加、减、乘、除法运算,输入文本框显示为常量多项式,输出文本框显示为精确到小数点后十位的数值。

计算器包含十个数字键0~9、小数点键“.”、小括号键“(”“)”、运算符号、清屏键、退格键、退出键等。

二、设计思路:每按下一个数字键或者符号键的时候,利用get(handles.edit1,'string')获取当前输入文本框edit1的string属性,并通过字符串合并函数strcat()将当前输入文本框edit1的字符型string属性与按键的字符型string属性合并成字符串,然后利用属性设置函数set(); 将合并后的字符串返回到输入文本框edit1的string属性。

当输入结束,用户按下等号的时候,采用x=get(handles.edit1,'string')获取当前输入文本框edit1的string属性,然后利用字符串执行命令y1=eval(x)计算输入文本框的表达式,得到的结果是字符型常量;然后利用字符串输出格式控制函数y=sprintf('%.10f',y1)使输出结果精确到小数点后十位;最后利用属性设置函数set(handles.edit2,'string',y)将转换后的字符串返回到输出文本框edit2的string 属性。

三、设计步骤:1、运行matlab 7.13版,进入主界面。

2、点击工具栏上的GUIDE图标进入GUI开发环境。

3、创建控件:Push Button包括数字0~9、运算符“+ - ×÷”、功能键等十九个,输入和动态静态文本框Edit Text,显示文字的静态文本框Static Text四个等。

4、对控件单击右键弹出菜单,选择Property Inspector,设置控件属性。

控件属性表text text1 制作:***16pointsboldon0.8310.8160.7840 0 1 text2 Inputtext3 Outputtext4 算术计算器200.7490.749editedit1 空白字符串10pointson 1 1 1 0 0 0 edit2 空白字符串5、选择工具栏上的Align Objects按键,调整控件布局。

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图形用户界面的部分控件的使用方法。

矩阵计算器

矩阵计算器

用Matlab GUI编写一个简单的矩阵计算器摘要:矩阵是线性代数的一个主要内容,又是解决众多问题的主要工具。

而矩阵运算是矩阵理论的基本内容之一。

目前,普通的计算器只能够进行数的运算,而不能够实现矩阵的运算。

随着科技的不断发展,人们对矩阵的运用也会不断的增多。

因此,制作一个矩阵计算器对当今科技的发展有一定的推动作用。

关键字:矩阵运算、Matlab GUI、计算器正文:一、矩阵的相关知识在数学上,矩阵是指纵横排列的二维数据表格,最早来自于方程组的系数及常数所构成的方阵。

这一概念由19世纪英国数学家凯利首先提出。

矩阵,是由个数组成的一个行列的矩形表格,通常用大写字母表示,组成矩阵的每一个数,均称为矩阵的元素,通常用小写字母其元素表示,其中下标都是正整数,他们表示该元素在矩阵中的位置。

比如,或表示一个矩阵,下标表示元素位于该矩阵的第行、第列。

元素全为零的矩阵称为零矩阵。

当一个矩阵的行数与烈数相等时,该矩阵称为一个阶方阵。

对于方阵,从左上角到右下角的连线,称为主对角线;而从左下角到右上角的连线称为付对角线。

若一个阶方阵的主对角线上的元素都是,而其余元素都是零,则称为单位矩阵,记为,即:。

如一个阶方阵的主对角线上(下)方的元素都是零,则称为下(上)三角矩阵,例如,是一个阶下三角矩阵,而则是一个阶上三角矩阵。

今后我们用表示数域上的矩阵构成的集合,而用或者表示数域上的阶方阵构成的集合。

矩阵是高等代数学中的常见工具,其中的一个重要用途是解线性方程组。

线性方程组中未知量的系数可以排成一个矩阵,加上常数项,则称为增广矩阵。

另一个重要用途是表示线性变换,即是诸如f(x) 4x之类的线性函数的推广。

同时也常见于统计分析等应用数学学科中。

在物理学中,矩阵于电路学、力学、光学和量子物理中都有应用;计算机科学中,三维动画制作也需要用到矩阵。

矩阵的运算是数值分析领域的重要问题。

将矩阵分解为简单矩阵的组合可以在理论和实际应用上简化矩阵的运算。

用MATLAB做的 一个计算器

用MATLAB做的 一个计算器

一、布局GUI。

1.打开Matlab,输入Guide 回车或者在工具栏上点击图标出现Guide 窗口:2.然后双击“Blank GUI(Default)”出现GUI窗口3.添加按钮4.根据按钮的作用及视觉效果做一定的修改把按钮的字符串大小、颜色进行设置,对按钮的位置进行排布,尽量使按钮集中在静态文本框下面。

最终设置的静态文本框为白色,其他按钮均为分红色。

5.保存、添加功能函数把做好的按钮及静态文本框保存后自动弹出Editor的M文本,对然后对相应的pushbutton添加功能函数。

以下是相应按钮的功能函数。

(1)数字按键编写。

在function pushbutton1_Callback(hObject, eventdata, handles)下输入:textString = get(handles.text1,'String');textString =strcat(textString,'0');set(handles.text1,'String',textString)这是使用句柄handles指向对象text1,并以字符串形式来存储数据文本框text1的内容,并存储数个“0”,然后由set(handles.text1,'String','textString'在text1中输出。

同理,分别在function pushbutton2~10_Callback(hObject, eventdata, handles)下给1~9数字按键下编写此类程序。

(2).符号键:function pushbutton12_Callback(hObject, eventdata, handles)textString = get(handles.text1,'String');textString =strcat(textString,'+');set(handles.text1,'String',textString)strcat的作用是将两个字符串连接起来,就是在已输入的存储数据textString 后添加“+”进行运算。

实现一个简单的计算器程序

实现一个简单的计算器程序

实现一个简单的计算器程序计算器是我们日常生活中不可或缺的工具,方便快捷。

而如今,多种多样的计算器软件层出不穷,但是否可以自己动手制作一个基础的计算器程序呢?实际上,这并不难以实现。

一、确定计算器功能想要实现一个简单的计算器程序,首先需要确定计算器应有的基础功能。

一般来说,计算器至少应该具备加、减、乘、除这几种基本运算功能,以及清空、删除、等于等辅助操作功能。

二、选择合适的编程语言每一种编程语言都有自己的特点和适用场景,我们需要根据自己的需求来选择一种合适的编程语言。

比较常用的编程语言有C++、Java、Python等,在这里我们以Python为例进行讲解。

三、使用Tkinter库创建界面Tkinter是Python自带的一个GUI库,可以用来创建应用程序的图形用户界面,也是实现计算器界面的一种常用方式。

首先我们需要导入Tkinter库:```from tkinter import *```接着,我们可以创建一个窗口并设置相关属性:```window = Tk()window.title("计算器")```设置窗口标题为“计算器”。

四、实现计算器功能在创建好界面之后,我们需要实现计算器的具体功能。

首先,我们需要定义一些基本的操作函数,如加、减、乘、除等操作函数:```# 加法函数def add(num1, num2):return num1 + num2# 减法函数def subtract(num1, num2):return num1 - num2# 乘法函数def multiply(num1, num2):return num1 * num2# 除法函数def divide(num1, num2):if num2 == 0:return "错误!除数不能为0!" else:return num1 / num2```接着,我们可以定义一些辅助函数,如清空、删除等操作函数:```# 清空函数def clear():entry.delete(0, END)# 删除函数def delete():entry.delete(len(entry.get())-1, END)```在设置好函数之后,我们可以将其与具体的界面元素进行关联,实现计算器的完整功能:```# 数字按钮button_1 = Button(window, text="1", padx=20, pady=10, command=lambda: entry.insert(END, "1"))button_2 = Button(window, text="2", padx=20, pady=10, command=lambda: entry.insert(END, "2"))...button_9 = Button(window, text="9", padx=20, pady=10, command=lambda: entry.insert(END, "9"))button_0 = Button(window, text="0", padx=20, pady=10, command=lambda: entry.insert(END, "0"))# 运算符按钮button_add = Button(window, text="+", padx=19, pady=10, command=lambda: operation("+"))button_subtract = Button(window, text="-", padx=20, pady=10, command=lambda: operation("-"))button_multiply = Button(window, text="*", padx=20, pady=10, command=lambda: operation("*"))button_divide = Button(window, text="/", padx=20, pady=10, command=lambda: operation("/"))# 其他辅助按钮button_clear = Button(window, text="清空", padx=60, pady=10, command=clear)button_delete = Button(window, text="删除", padx=60, pady=10, command=delete)button_equal = Button(window, text="=", padx=20, pady=10, command=equal)# 文本框entry = Entry(window, width=40, borderwidth=5)# 布局entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)button_7.grid(row=1, column=0, padx=5, pady=5)button_8.grid(row=1, column=1, padx=5, pady=5)button_9.grid(row=1, column=2, padx=5, pady=5)button_add.grid(row=1, column=3, padx=5, pady=5)...button_clear.grid(row=4, column=0, columnspan=2, padx=5,pady=5)button_0.grid(row=4, column=2, padx=5, pady=5)button_equal.grid(row=4, column=3, padx=5, pady=5)```最后,运行程序,就可以看到一个简单的计算器窗口了。

matlab gui计算器工作原理

matlab gui计算器工作原理

MATLAB GUI计算器是一种基于MATLAB编程语言的图形用户界面应用程序,它可以像普通计算器一样执行基本数学运算并提供一些高级功能。

下面将介绍MATLAB GUI计算器的工作原理。

1. MATLAB GUI计算器的界面设计MATLAB GUI计算器的界面主要由各种按钮、文本框和其他控件组成,用户可以通过鼠标点击按钮或者输入文本框来进行计算操作。

界面设计需要考虑美观和易用性,通常可以使用MATLAB中的GUIDE工具来进行可视化设计。

2. MATLAB GUI计算器的程序逻辑MATLAB GUI计算器的程序逻辑主要由MATLAB脚本文件编写而成,这些脚本文件包括界面初始化、按钮点击事件处理、计算逻辑等。

通过编写MATLAB脚本,可以实现各种计算功能,并与界面上的控件进行交互。

3. MATLAB GUI计算器的计算功能MATLAB GUI计算器可以执行基本的加减乘除运算,同时还可以实现一些高级的数学函数计算,比如三角函数、指数函数、对数函数等。

这些功能需要通过MATLAB内置的数学函数库来实现。

4. MATLAB GUI计算器的数据输入和输出MATLAB GUI计算器的数据输入通常通过用户的按钮点击或者键盘输入来实现,而数据输出则通过界面上的文本框显示。

在实现计算逻辑时,可以将用户输入的数据和计算结果通过MATLAB脚本进行处理,并更新到界面上。

5. MATLAB GUI计算器的实时更新MATLAB GUI计算器可以在用户输入数据或者点击按钮时实时更新界面上的数据和计算结果,这需要通过MATLAB脚本中的事件处理函数来实现数据更新和界面刷新。

通过上述介绍,我们可以清晰地了解MATLAB GUI计算器的工作原理和实现方法。

通过深入学习MATLAB编程语言和GUI设计技术,我们可以设计出更加复杂和功能强大的GUI应用程序,为科学计算和工程应用提供便利和高效的计算工具。

6. MATLAB GUI计算器的错误处理在使用MATLAB GUI计算器时,用户可能会输入错误的数据或者进行非法的计算操作,因此在程序中需要考虑到错误处理的逻辑。

在Matlab中使用GUI工具箱进行界面设计

在Matlab中使用GUI工具箱进行界面设计

在Matlab中使用GUI工具箱进行界面设计Matlab是一款功能强大的科学计算软件,它不仅可以进行数值计算和数据分析,还提供了用于图形用户界面(Graphical User Interface,简称GUI)的工具箱。

通过GUI工具箱,用户可以设计出直观、易用的界面,方便自己和他人使用和操作自己编写的程序。

GUI工具箱提供了丰富的可视化组件,如按钮、文本框、下拉菜单等,以及一系列的回调函数,使得在Matlab环境中进行GUI设计变得相对简单。

下面我将简单介绍一下在Matlab中使用GUI工具箱进行界面设计的流程和常用技巧。

首先,打开Matlab软件,创建一个新的GUI应用程序。

在Matlab命令窗口中输入"guide"命令,即可打开GUI编辑器。

在GUI编辑器中,可以选择默认的空白界面或基于模板的预设界面进行设计。

在设计界面之前,需要明确自己的需求和设计目标。

例如,设计一个简单的计算器界面,包含输入框、按钮和显示框等组件。

接下来,可以通过拖拽和放置组件等方式来设计界面。

在GUI编辑器的左侧可以看到工具箱,其中包含了各种可用的界面组件。

不同组件有不同的属性,可以通过调整属性值来实现个性化的设计。

例如,我们可以在界面上放置一个输入框组件,用于用户输入计算器的操作数。

在属性编辑器中,可以设置输入框的位置、大小、默认值等属性。

同样地,我们可以在界面上放置一个按钮组件,用于用户点击进行计算。

在属性编辑器中,可以设置按钮的文本、位置、大小等属性。

在设计完界面之后,需要为按钮组件添加回调函数,以响应用户的操作。

回调函数是一种特殊的函数,当用户与界面交互时会被调用。

回调函数可以通过访问界面上的组件来获取用户输入的数据,并进行相应的计算和操作。

在Matlab中,可以通过编辑按钮的"Callback"属性来设置回调函数。

例如,我们可以为计算按钮添加如下的回调函数:function calculateButton_Callback(hObject, eventdata, handles)operand = str2double(get(handles.inputBox, 'String'));result = calculate(operand);set(handles.outputBox, 'String', num2str(result));上述回调函数中,首先通过获取输入框组件的字符串,将用户输入的操作数转换为浮点数。

基于MATLAB的多功能计算器设计与实现

基于MATLAB的多功能计算器设计与实现

基于MATLAB的多功能计算器设计与实现摘要随看信息技术的发展,数据变得越来越重要,针对数据的处理也越来越复杂,计算器在我们日常生活学习中扮演越来越重要角色。

本文基于MATLAB设计的GUI 多功能计算器,包括三部分:简易计算器、科学计算器和程序员计算器。

其中,简易计算器包括了常用的加减乘除;科学计算器在加减乘除上,添加了正余弦、阶乘和对数等常用的数学计算;程序员计算器包括了二进制、八进制、十六进制的相互转换和二进制的与、或和异或等逻辑运算。

此多功能计算器改进了以前功能单一的计算器,具有较强的实用性。

关键字:MATLAB; GUI ;多功能计算器AbstractWith the development of information technology, the data become more andmore complicated, according to data operations have become increasingly frequent, thecalculator is playing a more and more important role in leamning in our daily life. In this paper,MATLAB design of GUI based multi functional calculator, consists of three parts: a simplecalculator, scientific calculator and programmers calculator. The simple calculator, includingcommon add, subtract, multiply and divide; scientific calculator in add, subtract, multiply anddivide, add the commonly used mathematical cosine, factorial and logarithm calculation;programmers calculator includes the conversion of binary, octal, hexadecimal and binarysixteen and, or and XOR logic operation. The multi-function calculator before improvedsingle function has a strong practical.Keyword: MATLAB; GUI; Multi Function Calculator目录第1章绪论 (1)1.1.基于MATLAB的多功能计算器设计的目的和意义 (1)1.2国内外现状分析 (1)1.2.1 国内外计算器的发展 (1)1.2.2计算器的类型 (2)第2章数字信号处理原理概念 (4)2.1数字信号处理 (4)2.2数字信号处理的概述 (4)2.3用MATLAB实现信号处理 (4)2.3.1信号的取样 (4)2.3.2信号的重构 (5)第3章计算器总体设计 (6)3.1计算器整体设计思路 (6)3.2计算器的功能实现 (7)3.2.1数字0-9设计与实现 (7)3.2.2四则运算设计 (7)3.2.3简单科学计算设计 (7)3.2.4功能按钮设计 (7)第4章 MATLAB GUI界面设计 (8)4.1MATLAB GUI介绍 (8)4.2GUI界面创建的基本知识 (8)4.3计算器界面设计 (9)第5章 MATLAB程序设计 (10)5.1 M文件 (10)5.1.1局部变量与全局变量 (10)5.1.2M文件的编辑与运行 (11)5.1.3脚本文件 (13)5.1.4函数文件 (13)5.1.5函数调用 (14)5.2MATLAB的程序控制结构 (16)5.3 数据的输入与输出 (17)5.3.1 键盘输入语句(input) (17)5.3.2屏幕输出语句(disp) (17)5.3.3 M数据文件的存储/加载(save/load) (17)5.3.4二进制数据文件的存储/读取 (18)5.4.面向对象程序设计的基本方法 (19)5.4.1.创建类目录 (19)5.4.2.建立类的数据结构 (19)5.4.3创建类的基本方法 (19)5.4.4重载运算 (19)5.4.5面向对象的函数 (19)5.5MATLAB程序优化 (19)结语 (20)附录 (21)参考文献 (28)致谢...............................................................................................................第1章绪论1.1.基于MATLAB的多功能计算器设计的目的和意义当今时代是一个信息化的时代,信息化的时代到处充满了数据,生活在这个信息化时代的人们,无论人们在做什么行业,都需要时时刻刻跟数据打交道。

实验8MATLAB图形用户界面设计实验报告

实验8MATLAB图形用户界面设计实验报告

实验报告课程名称:MATLAB 应用实验项目名称:MATLAB 图形用户界面设计 实验时间:11.6班级:XXXXX 姓名:XXXXX 学号:XXX实 验 目 的:1. 熟悉MATLAB 的菜单设计方法2. 熟悉MATLAB 的主要控件使用方法3. 熟悉MATLAB 的GUI 设计流程实 验 环 境:MATLAB 7.1实 验 内 容 及 过 程:简单计算器的设计:在MATLAB GUI 的实验环境中,通过MATLAB 图形用户界面设计的方法,设计一个简单的计算器,以实现简单数据的退出、清屏、删除、四则运算等基本功能。

实 验 过 程、结 果 及 分 析:计算器代码:function varargout = jsq(varargin)gui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...成 绩: 指导教师(签名):'gui_OpeningFcn', @jsq_OpeningFcn, ...'gui_OutputFcn', @jsq_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{:});endfunction jsq_OpeningFcn(hObject, eventdata, handles, varargin)guidata(hObject, handles);function varargout = jsq_OutputFcn(hObject, eventdata, handles)varargout{1} = handles.output;function edit1_Callback(hObject, eventdata, handles)function edit1_CreateFcn(hObject, eventdata, handles)if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');endfunction pushbutton19_Callback(hObject, eventdata, handles)close(gcf);function pushbutton17_Callback(hObject, eventdata, handles)set(handles.edit1,'String','0');function pushbutton18_Callback(hObject, eventdata, handles)global a;a=get(handles.edit1,'String');switch acase ' 'b='0';case '0'b=a;otherwiseb=a(1:length(a)-1);endset(handles.edit1,'String',b);guidata(hObject, handles);% --- Executes on button press in pushbutton4.function pushbutton16_Callback(hObject, eventdata, handles)global a;global b;b=strcat(b,strcat(a,'+'));set(handles.edit1,'String','0');function pushbutton15_Callback(hObject, eventdata, handles)global a;global b;b=strcat(b,strcat(a,'-'));set(handles.edit1,'String','0');function pushbutton14_Callback(hObject, eventdata, handles) global a;a=get(handles.edit1,'String');if a=='0';a='9';elsea=strcat(a,'9');%字符串连接endset(handles.edit1,'String',a);guidata(hObject, handles);function pushbutton13_Callback(hObject, eventdata, handles) global a;a=get(handles.edit1,'String');if a=='0';a='8';elsea=strcat(a,'8');%字符串连接endset(handles.edit1,'String',a);guidata(hObject, handles);function pushbutton12_Callback(hObject, eventdata, handles) global a;a=get(handles.edit1,'String');if a=='0';a='7';elsea=strcat(a,'7');%字符串连接endset(handles.edit1,'String',a);guidata(hObject, handles);function pushbutton11_Callback(hObject, eventdata, handles) global a;a=get(handles.edit1,'String');if a=='0';a='4';elsea=strcat(a,'4');%字符串连接endset(handles.edit1,'String',a);guidata(hObject, handles);function pushbutton10_Callback(hObject, eventdata, handles) global a;a=get(handles.edit1,'String');if a=='0';a='5';elsea=strcat(a,'5');%字符串连接endset(handles.edit1,'String',a);guidata(hObject, handles);function pushbutton9_Callback(hObject, eventdata, handles) global a;a=get(handles.edit1,'String');if a=='0';a='6';elsea=strcat(a,'6');%字符串连接endset(handles.edit1,'String',a);guidata(hObject, handles);function pushbutton8_Callback(hObject, eventdata, handles) global a;global b;b=strcat(b,strcat(a,'*'));set(handles.edit1,'String','0');function pushbutton7_Callback(hObject, eventdata, handles) global a;global b;b=strcat(b,strcat(a,'/'));set(handles.edit1,'String','0');function pushbutton6_Callback(hObject, eventdata, handles) global a;a=get(handles.edit1,'String');if a=='0';a='3';elsea=strcat(a,'3');%字符串连接endset(handles.edit1,'String',a);guidata(hObject, handles);function pushbutton5_Callback(hObject, eventdata, handles) global a;if a=='0';a='2';elsea=strcat(a,'2');%字符串连接endset(handles.edit1,'String',a);guidata(hObject, handles);function pushbutton4_Callback(hObject, eventdata, handles) global a;a=get(handles.edit1,'String');if a=='0';a='1';elsea=strcat(a,'1');%字符串连接endset(handles.edit1,'String',a);guidata(hObject, handles);function pushbutton3_Callback(hObject, eventdata, handles) global a;a=get(handles.edit1,'String');if a=='0';a='0';elsea=strcat(a,'0');%字符串连接endset(handles.edit1,'String',a);guidata(hObject, handles);function pushbutton2_Callback(hObject, eventdata, handles) global a;a=get(handles.edit1,'String');b=strfind(a,'.');if numel(b)==0a=strcat(a,'.');elseset(handles.edit1,'String',a);endset(handles.edit1,'String',a);guidata(hObject, handles);function pushbutton1_Callback(hObject, eventdata, handles) global a;global b;c=get(handles.edit1,'String');d=strcat(b,c);a=eval(d);b='0'; %避免出现累加问题a=num2str(a);guidata(hObject, handles);。

gui计算器设计matlab代码

gui计算器设计matlab代码

GUI计算器设计MATLAB代码1. 简介GUI(Graphical User Interface,图形用户界面)计算器是一种利用图形界面设计的计算器,用户可以通过鼠标点击按钮进行数字输入和运算。

MATLAB是一种功能强大的数学计算软件,拥有丰富的绘图和可视化功能,可以用于设计和实现GUI计算器。

本文将介绍如何使用MATLAB编写GUI计算器的代码。

2. 界面设计在设计GUI计算器的界面时,需要考虑用户友好性和美观性。

可以通过MATLAB提供的GUIDE工具进行界面设计,添加数字按钮、运算符按钮和显示屏等组件。

另外,还可以设置布局和颜色,使界面更加美观。

3. 代码实现需要创建一个新的MATLAB GUI应用程序,然后在GUIDE工具中设计界面。

在界面设计完成后,需要编辑回调函数来实现计算器的功能。

以下是一个简单的GUI计算器的MATLAB代码示例:```matlabfunction calculatorGUI创建整体界面figure('Name','Calculator','NumberTitle','off','Position',[100,100,4 00,600]);创建显示屏hEdit =uicontrol('Style','edit','String','','Position',[50,500,300,50]);创建数字按钮numBtns = {'7','8','9','4','5','6','1','2','3','0'};for i = 1:10hNumBtn(i) =uicontrol('Style','pushbutton','String',numBtns{i},'Position',[50+5 0*rem(i-1,3),450-50*fix((i-1)/3),50,50],'Callback',numBtn_callback);end创建运算符按钮operatorBtns = {'+','-','*','/','='};for i = 1:5hOperatorBtn(i) =uicontrol('Style','pushbutton','String',operatorBtns{i},'Position',[2 00,450-50*(i-1),50,50],'Callback', operatorBtn_callback);数字按钮回调函数function numBtn_callback(hObject,~)str = get(hEdit,'String');str = [str get(hObject,'String')];set(hEdit,'String',str);end运算符按钮回调函数function operatorBtn_callback(hObject,~) str = get(hEdit,'String');val = get(hObject,'String');if ~isempty(str)if strcmp(val,'=')tryresult = eval(str);set(hEdit,'String',num2str(result)); catchset(hEdit,'String','Error');endelseset(hEdit,'String',[str val]);endendendend```4. 运行效果运行以上代码,将会弹出一个简单的GUI计算器界面,用户可以通过点击数字按钮和运算符按钮来输入表达式并进行计算。

matlab gui实例

matlab gui实例

matlab gui实例MATLAB GUI实例MATLAB GUI是一种基于图形用户界面的交互式工具,它可以帮助用户快速创建自己的图形界面,并且可以通过简单的拖放操作来实现各种功能。

在本文中,我们将介绍一个简单的MATLAB GUI实例,以帮助初学者更好地理解和掌握这一工具。

首先,我们需要创建一个新的MATLAB GUI。

在MATLAB主界面中,选择“新建”->“GUI”->“App Designer”,然后选择“Blank App”模板。

接下来,我们需要添加一些基本的组件,例如按钮、文本框和列表框。

这些组件可以通过拖放操作从左侧的“组件”面板中添加到GUI窗口中。

在本例中,我们将创建一个简单的计算器应用程序,用户可以通过输入两个数字和选择一个操作符来计算结果。

因此,我们需要添加两个文本框用于输入数字,一个列表框用于选择操作符,一个按钮用于触发计算操作,并且一个文本框用于显示结果。

这些组件可以通过以下代码添加到GUI窗口中:```matlabclassdef Calculator < matlab.apps.AppBase% Properties that correspond to app componentsproperties (Access = public)UIFigure matlab.ui.FigureNumber1EditField matlab.ui.control.NumericEditField Number2EditField matlab.ui.control.NumericEditField OperatorListBox matlab.ui.control.ListBoxCalculateButton matlab.ui.control.ButtonResultEditField matlab.ui.control.NumericEditField end% Callbacks that handle component eventsmethods (Access = private)% Button pushed function: CalculateButtonfunction CalculateButtonPushed(app, event)% Get the input valuesnum1 = app.Number1EditField.Value;num2 = app.Number2EditField.Value;op = app.OperatorListBox.Value;% Calculate the resultswitch opcase '+'result = num1 + num2;case '-'result = num1 - num2;case '*'result = num1 * num2;case '/'result = num1 / num2;end% Display the resultapp.ResultEditField.Value = result; endend% App initialization and constructionmethods (Access = private)% Create UIFigure and componentsfunction createComponents(app)% Create UIFigure and set propertiesapp.UIFigure = uifigure('Name', 'Calculator');app.UIFigure.Position = [100 100 640 480];% Create Number1EditFieldapp.Number1EditField = uieditfield(app.UIFigure,'numeric');app.Number1EditField.Position = [50 400 100 22];% Create Number2EditFieldapp.Number2EditField = uieditfield(app.UIFigure,'numeric');app.Number2EditField.Position = [200 400 100 22];% Create OperatorListBoxapp.OperatorListBox = uilistbox(app.UIFigure);app.OperatorListBox.Items = {'+', '-', '*', '/'};app.OperatorListBox.Position = [350 400 100 80];app.OperatorListBox.Value = '+';% Create CalculateButtonapp.CalculateButton = uibutton(app.UIFigure, 'push'); app.CalculateButton.ButtonPushedFcn = createCallbackFcn(app, @CalculateButtonPushed, true);app.CalculateButton.Position = [500 400 100 22];app.CalculateButton.Text = 'Calculate';% Create ResultEditFieldapp.ResultEditField = uieditfield(app.UIFigure, 'numeric'); app.ResultEditField.Position = [50 300 100 22];app.ResultEditField.Value = 0;endend% App creation and deletionmethods (Access = public)% Construct appfunction app = Calculator% Create UIFigure and componentscreateComponents(app);% Show the figure after all components are createdapp.UIFigure.Visible = 'on';endendend```在上面的代码中,我们定义了一个名为“Calculator”的类,它继承自“matlab.apps.AppBase”类。

Matlab实现的简易计算器程序代码

Matlab实现的简易计算器程序代码

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_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application % stop. All inputs are passed 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% Edit the above text to modify the response to help jisuanqi% Last Modified by GUIDE v2.5 20-Jul-2011 09:45:20% 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_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 jisuanqi is made visible.function jisuanqi_OpeningFcn(hObject, eventdata, handles, varargin) handles.current_str='';%´ËΪ´æ´¢µ±Ç°µÄ×Ö·û´®handles.L1_str='';handles.L2_str='';%´ËΪ´æ´¢ÉÏÒ»¸öÊý×ÖµÄ×Ö·û´®% 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 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);% --- Outputs from this function are returned to the command line. function varargout = 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 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 Number_0.function Number_0_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ0£¬Èç¹û°´ÁË0¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'0');handles.L1_str=strcat(handles.L1_str,'0');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_0 (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 xiaoshudian.function xiaoshudian_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'.');guidata(hObject, handles);%СÊýµã% hObject handle to xiaoshudian (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 jia_jian.function jia_jian_Callback(hObject, eventdata, handles)% hObject handle to jia_jian (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 Number_1.function Number_1_Callback(hObject, eventdata, handles)% hObject handle to Number_1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ1£¬Èç¹û°´ÁË1¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'1');handles.L1_str=strcat(handles.L1_str,'1');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% --- Executes on button press in Number_2.function Number_2_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ2£¬Èç¹û°´ÁË1¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'2');handles.L1_str=strcat(handles.L1_str,'2');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_2 (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 Number_3.function Number_3_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ3£¬Èç¹û°´ÁË3¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'3');handles.L1_str=strcat(handles.L1_str,'3');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_3 (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 Number_4.function Number_4_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ4£¬Èç¹û°´ÁË4¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'4');handles.L1_str=strcat(handles.L1_str,'4');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_4 (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 Number_5.function Number_5_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ5£¬Èç¹û°´ÁË5¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'5');handles.L1_str=strcat(handles.L1_str,'5');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_5 (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 Number_6.function Number_6_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ6£¬Èç¹û°´ÁË6¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'6');handles.L1_str=strcat(handles.L1_str,'6');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_6 (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 Number_7.function Number_7_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ7£¬Èç¹û°´ÁË7¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'7');handles.L1_str=strcat(handles.L1_str,'7');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_7 (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 Number_8.function Number_8_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ8£¬Èç¹û°´ÁË8¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'8');handles.L1_str=strcat(handles.L1_str,'8');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_8 (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 Number_9.function Number_9_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ9£¬Èç¹û°´ÁË9¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'9');handles.L1_str=strcat(handles.L1_str,'9');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_9 (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 jiahao.function jiahao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'+');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to jiahao (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 denghao.function denghao_Callback(hObject, eventdata, handles)st = get(handles.edit1,'String');%¼ÆËã½á¹û£¬²¢´æ·Åµ½µÚ¶þ¸ö±à¼-¿òÖС£val = eval(st);s = num2str(val);set(handles.edit2,'String',s);guidata(hObject, 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)% --- Executes on button press in jianhao.function jianhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'-');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to jianhao (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 X_daoshu.function X_daoshu_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'\1');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to X_daoshu (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 chenghao.function chenghao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'*');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to chenghao (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 baifenhao.function baifenhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'/100');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);handles.L2_str=handles.L1_str;handles.L1_str = '';% hObject handle to baifenhao (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 chuhao.function chuhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'/');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to chuhao (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 Sqrt.function Sqrt_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'sqrt');handles.L1_str=strcat(handles.L1_str,'sqrt');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Sqrt (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 X_lifang.function X_lifang_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'^3');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to X_lifang (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 x_pingfang.function x_pingfang_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'^2');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to x_pingfang (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 zhengqie.function zhengqie_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'tan');handles.L1_str=strcat(handles.L1_str,'tan');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to zhengqie (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 zhengxian.function zhengxian_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'sin');handles.L1_str=strcat(handles.L1_str,'sin');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to zhengxian (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 CE.function CE_Callback(hObject, eventdata, handles)% hObject handle to CE (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 C.function C_Callback(hObject, eventdata, handles)% hObject handle to C (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.current_str = '';set(handles.edit1,'String','0.');set(handles.edit2,'String','0.');guidata(hObject, handles);% --- Executes on button press in Backspace.function Backspace_Callback(hObject, eventdata, handles)% hObject handle to Backspace (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 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 && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endif ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')) ;end% --- Executes on button press in shujujiazai.function shujujiazai_Callback(hObject, eventdata, handles)% hObject handle to shujujiazai (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global YSData;[filename,pathname,FILTERINDEX]=uigetfile({'*.xls';'*.dat';'*.*'},'Ñ¡ÔñÊý¾ÝÎļþ');if(FILTERINDEX==0)return;endstr_filename=[pathname,filename];fid=fopen(str_filename,'rt');if(fid==-1)errordlg('Open file error!','Open error');return;endYSData=xlsread(str_filename);set(handles.lujingxianshi,'String',str_filename);set(handles.tuxingxianshi,'Enable','on');function lujingxianshi_Callback(hObject, eventdata, handles)% hObject handle to lujingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.lujingxianshi,'String',pathname);% Hints: get(hObject,'String') returns contents of lujingxianshi as text % str2double(get(hObject,'String')) returns contents of lujingxianshi as a double% --- Executes during object creation, after setting all properties. function lujingxianshi_CreateFcn(hObject, eventdata, handles)% hObject handle to lujingxianshi (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% --- Executes on button press in zhuxingtu.function zhuxingtu_Callback(hObject, eventdata, handles)% hObject handle to zhuxingtu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',1);set(handles.zhexiantu,'Value',0);set(handles.yuanbingtu,'Value',0);% Hint: get(hObject,'Value') returns toggle state of zhuxingtu% --- Executes on button press in zhexiantu.function zhexiantu_Callback(hObject, eventdata, handles)% hObject handle to zhexiantu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',0);set(handles.zhexiantu,'Value',1);set(handles.yuanbingtu,'Value',0);% Hint: get(hObject,'Value') returns toggle state of zhexiantu% --- Executes on button press in yuanbingtu.function yuanbingtu_Callback(hObject, eventdata, handles)% hObject handle to yuanbingtu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',0);set(handles.zhexiantu,'Value',0);set(handles.yuanbingtu,'Value',1);% Hint: get(hObject,'Value') returns toggle state of yuanbingtu% --- Executes on button press in tuxingxianshi.function tuxingxianshi_Callback(hObject, eventdata, handles)global YSData;%Åжϡ®Í¼ÏñÀàÐÍ¡¯ÏÂ×éºÏ¿òÖеĸ÷µ¥Ñ¡°´Å¥µÄÑ¡ÖÐÇé¿ö£¬²¢´æ´¢ÔÚIndex_rad iobuttonÖÐIndex_radiobutton=get([handles.zhuxingtu,handles.zhexiantu,handles.yuan bingtu],'Value');%½øÐÐͼÐÎÉú³Éif(Index_radiobutton{1}==1)bar(YSData);title('¸÷Êý¾ÝÖ±·½Í¼');endif(Index_radiobutton{2}==1)plot(YSData);title('¸÷Êý¾ÝÕÛÏßͼ');endif(Index_radiobutton{3}==1)pie(YSData);title('¸÷Êý¾ÝÔ²±ýͼ');end% hObject handle to tuxingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% global YSData;% --- Executes during object creation, after setting all properties. function tuxingxianshi_CreateFcn(hObject, eventdata, handles)% hObject handle to tuxingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called function 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% --- Executes on button press in Quit.function Quit_Callback(hObject, eventdata, handles)% hObject handle to Quit (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)close(gcf);% --- Executes during object deletion, before destroying properties. function zhengxian_DeleteFcn(hObject, eventdata, handles)% hObject handle to zhengxian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)function edit3_Callback(hObject, eventdata, handles)% hObject handle to edit3 (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 edit3 as text% str2double(get(hObject,'String')) returns contents of edit3 as a double% --- Executes during object creation, after setting all properties. function edit3_CreateFcn(hObject, eventdata, handles)% hObject handle to edit3 (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。

MATLABGUI入门简单实例(加法计算器)

MATLABGUI入门简单实例(加法计算器)

本人初学GUI ,针对网上版本的混乱,学得一头雾水,特重新整理如下,供大家参考。

1.首先我们新建一个 GUI 文件:File/New/GUI 如下图所示;选择 Blank GUI(Default),点击 OK 。

2.进入GUI 开发环境以后添加 3个编辑文本框(Edit Text ),2个静态文本框(Static Text ), 和一个Push Button 按钮,布置如下图所示;⑷修改Pushbutton 按钮的属性,“ string 改为“ Add ” “ tag 改为“ add ”⑸另外2个Static 文本就是的“ string 修改为"+ ”"=”(6)点击“保存”,名字任意取,自动弹出 M 文件(我的是7.0版本,不知其他版本会不 会如此)。

(1) 第一个 “in putl ”(2) 第二个 “input2 ”(3)第三个“ Edit Text! ”的用来存储第一个加数,我们将“Edit Tex2”的用来存储第二个加数,我们将"Edit Tex3"的 “string 改为 “Q ” “tag 为““ string 改为 “ 0,” “ tag 改为 “ string 改为 “ 0” “ tag 改为 an swer3•我们先为第一个Edit Text文本框添加代码,即读取第一个加数;点击上图所示红色方框,选择input1_Callback,光标便立刻移到下面这段代码(程序自动生成)的位置。

function in put1_Callback(hObject, even tdata, han dies)% hObject han dle to in putl (see GCBO)% eve ntdata reserved - to be defi ned in a future version of MATLAB% han dles structure with han dles and user data (see GUIDATA)% Hints: get(hObject, ' String ' ) returns cpnttlnasoext% str2double(get(hObject, ' String ' )) returnscontents of editl as a double然后在上面这段代码的下面插入如下代码:%以字符串的形式来存储数据文本框1的内容•如果字符串不是数字,则现实空白内容input=str2num(get(hObject,'String')); %检查输入是否为空.如果为空,则默认显示为0 if(isempty(i nput))set(hObject,'Stri ng','0')endguidata(hObject,handles);%保存handles结构体,只要在handles结构体有改变时,才需要保存%guidata(hObject, handles);%这里由于handles没有改变,故这里其实没有必要,但是为了避免潜在的不必要麻烦,建议在所有Callback都添加该命令。

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

基于MATLAB-GUI的简单计算器设计题目:计算器完成一个简单的计算器。

要求(但不限于):GUI上具有数字键盘输入区域,能够进行加、减、乘、除、三角函数等基础运算。

界面简洁、美观可能需要的控件:Push ButtonEdit TextPop-up Menu1 功能介绍本程序是一个简单的计算器程序,使用MATLAB软件编写完成。

主要具有加、减、乘、除、三角函数等基础运算,并通过GUI进行程序使用的交互。

程序交互界面如下:图1 程序的交互界面2 功能实现首先用MATLAB GUI功能,在绘制一个动态文本框和一个文本编辑框,以及25个命令按钮,调整好各控件大小、颜色,整体布局如图所示:备注:软件版本:MATLAB R2011b2.1 布局GUI1、打开MATLAB,在Guide中新建FIG文件2、然后双击“Blank GUI(Default)”或单击OK键出现GUI窗口3、添加按钮和动态文本框4、根据按钮的作用及视觉效果做一定的修改对按钮的字符串大小、颜色进行设置,对按钮的位置进行排布,尽量使按钮集中在动态文本框下面。

最终设置的动态文本框为灰色,其他按钮均为蓝色。

5、保存、添加功能函数将做好的按钮及动态文本框保存后自动弹出Editor的M文本,然后对相应的pushbutton添加功能函数。

以下是相应按钮的功能函数。

(1)数字按键编写。

在function pushbutton1_Callback(hObject, eventdata, handles)下输入:global jja=get(handles.edit1,'String');if(strcmp(a,'0.')==1)&&(jj==0)set(handles.edit1,'String','0.')elsea=strcat(a,'0')set(handles.edit1,'String',a)endjj=0这是使用句柄handles指向对象edit1,并以字符串形式来存储数据文本框edit1的内容,并存储数个“0”,然后由set(handles.edit1,'String',a)在edit1中输出。

同理,分别在function pushbutton2~10_Callback(hObject, eventdata, handles)下给1~9数字按键下编写此类程序。

(2)符号键:在function pushbutton11_Callback(hObject, eventdata, handles)下输入:global jjglobal ja=get(handles.edit1,'String')a=strcat(a,'+')if(jj==0)set(handles.edit1,'String',a)jj=1;endj=0;strcat的作用是将两个字符串连接起来,就是在已输入的存储数据a后添加“+”进行运算。

然后执行set(handles.edit1,'String',a)。

符号键‘-’、‘*’、‘/’、‘.’与‘+’的运算函数类似,“平方运算”主要是由“^2”功能实现。

function pushbutton12_Callback(hObject, eventdata, handles)global jjglobal ja=get(handles.edit1,'String')a=strcat(a,'-')if(jj==0)set(handles.edit1,'String',a)jj=1;endj=0;function pushbutton13_Callback(hObject, eventdata, handles) global jjglobal ja=get(handles.edit1,'String')a=strcat(a,'*')if(jj==0)set(handles.edit1,'String',a)jj=1;endj=0;function pushbutton14_Callback(hObject, eventdata, handles) global jjglobal ja=get(handles.edit1,'String')a=strcat(a,'/')if(jj==0)set(handles.edit1,'String',a)jj=1;endj=0;function pushbutton15_Callback(hObject, eventdata, handles) global jjglobal ja=get(handles.edit1,'String')a=strcat(a,'.')if(jj==0)set(handles.edit1,'String',a)jj=1;endj=0;function pushbutton16_Callback(hObject, eventdata, handles) a=get(handles.edit1,'String')b=eval(a)set(handles.edit1,'String',num2str(b^2))(3)运算符“=”的编程:a=get(handles.edit1,'string')b=eval(a)set(handles.edit1,'string',num2str(b))“eval”的作用是将符号表达式转换成数值表达式再由set(handles.edit1,'string',num2str(b))输出。

(4)按键“back”编程:即显示一个空字符:set(handles.edit1,'String',a)按键“back”编程:global jja=get(handles.edit1,'String');if(strcmp(a,'0.')==1)&(jj==0)set(handles.edit1,'String','0.')elsess=char(a);l=length(a);a=ss(1:l-1);set(handles.edit1,'String',a)endjj=0;(5)按键“清零”:把动态文本框的字符清空,返回一个空格。

set(handles.edit1,'String','0')(6)三角函数的编辑function pushbutton17_Callback(hObject, eventdata, handles)a=get(handles.edit1,'String');b=eval(a)b=b*pi/180;b=sin(b);set(handles.edit1,'String',b)b=b*pi/180是把角度转换为弧度,这样在编程环境中才能识别,sin才能起作用。

然后执行set函数,把结果输出来。

同理在cos,tan,cot的回调函数中也输入相应的函数,只需把b=sin(b);中的sin改为cos,tan,cot即可(7)按键“()”:在输入数据时添加括号,以便数据的优先计算。

global jja=get(handles.edit1,'String')if(strcmp(a,'0')==1)&&(jj==0)set(handles.edit1,'String','(')elsea=strcat(a,'(')set(handles.edit1,'String',a)endjj=0a=get(handles.edit1,'String')s1=strcat(a,')')set(handles.edit1,'String',s1)2.2 计算器的使用加法运算(+):按“=”后显示:减法(-)、乘法(*)、除法(/)运算与加法(+)运算类似。

点号(.)、括号(()):平方(X^2)运算:按下(X^2)后三角函数(sin、cos、tan、cot)运算:按下(sin、cos、tan、cot)后back、清零功能:3 程序总结本程序实现简单的科学运算功能及便捷的图形化交互界面。

具有以下优点:优点:1、GUI内数据传递非常简便。

非常简便的实现了前台与后台、前台与前台、后台与后台之间的参数传递。

2、图形化用户交互界面简洁明了。

在制作计算器界面时操作简单,制作完成后程序的输入框直接弹出,可以直接写入,即可运行计算器。

简单的实现了设计与编程的数据传递。

4 课程总结1、通过MATLAB简单计算器的设计,初步了解了关于MATLAB图形用户界面的部分控件的使用方法。

2、MATLAB的GUI提供的很多实用的控件,方便用于设计属于自己的图形界面。

3、Matlab具有强大、丰富的内置函数和工具箱,界面设计时更加简洁、快捷与直观。

5 参考文献[1] 《MATLAB语言及其在电子信息工程中的应用》王洪元主编清华大学出版社[2] 《MATLAB中GUI的应用》王洪元主编清华大学出版社附录(主要程序)function varargout = untitled66(varargin)% UNTITLED66 MATLAB code for untitled66.fig% UNTITLED66, by itself, creates a new UNTITLED66 or raises the existing % singleton*.%% H = UNTITLED66 returns the handle to a new UNTITLED66 or the handle to% the existing singleton*.%% UNTITLED66('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in UNTITLED66.M with the given input arguments. %% UNTITLED66('Property','Value',...) creates a new UNTITLED66 or raises the % existing singleton*. Starting from the left, property value pairs are% applied to the GUI before untitled66_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to untitled66_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% Edit the above text to modify the response to help untitled66% Last Modified by GUIDE v2.5 09-Dec-2014 20:42:08% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @untitled66_OpeningFcn, ...'gui_OutputFcn', @untitled66_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 untitled66 is made visible.function untitled66_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 untitled66 (see VARARGIN)% Choose default command line output for untitled66handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes untitled66 wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = untitled66_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;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 && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end% --- 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 jja=get(handles.edit1,'String');if(strcmp(a,'0.')==1)&&(jj==0)set(handles.edit1,'String','0.')elsea=strcat(a,'0')set(handles.edit1,'String',a)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 jja=get(handles.edit1,'String');if(strcmp(a,'0')==1)&&(jj==0)set(handles.edit1,'String','1')elsea=strcat(a,'1')set(handles.edit1,'String',a)jj=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 jja=get(handles.edit1,'String');if(strcmp(a,'0')==1)&&(jj==0)set(handles.edit1,'String','2')elsea=strcat(a,'2')set(handles.edit1,'String',a)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 jja=get(handles.edit1,'String');if(strcmp(a,'0')==1)&&(jj==0)set(handles.edit1,'String','3')elsea=strcat(a,'3')set(handles.edit1,'String',a)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 jja=get(handles.edit1,'String');if(strcmp(a,'0')==1)&&(jj==0)set(handles.edit1,'String','4')a=strcat(a,'4')set(handles.edit1,'String',a)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 jja=get(handles.edit1,'String');if(strcmp(a,'0')==1)&&(jj==0)set(handles.edit1,'String','5')elsea=strcat(a,'5')set(handles.edit1,'String',a)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 jja=get(handles.edit1,'String');if(strcmp(a,'0')==1)&&(jj==0)set(handles.edit1,'String','6')elsea=strcat(a,'6')set(handles.edit1,'String',a)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 jja=get(handles.edit1,'String');if(strcmp(a,'0')==1)&&(jj==0)set(handles.edit1,'String','7')elsea=strcat(a,'7')set(handles.edit1,'String',a)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 jja=get(handles.edit1,'String');if(strcmp(a,'0')==1)&&(jj==0)set(handles.edit1,'String','8')elsea=strcat(a,'8')set(handles.edit1,'String',a)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 jja=get(handles.edit1,'String');if(strcmp(a,'0')==1)&&(jj==0)set(handles.edit1,'String','9')elsea=strcat(a,'9')set(handles.edit1,'String',a)endjj=0% --- 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) global jjglobal ja=get(handles.edit1,'String')a=strcat(a,'+')if(jj==0)set(handles.edit1,'String',a)jj=1;endj=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 jjglobal ja=get(handles.edit1,'String')a=strcat(a,'-')if(jj==0)set(handles.edit1,'String',a)jj=1;endj=0;% --- 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) global jjglobal ja=get(handles.edit1,'String')a=strcat(a,'*')if(jj==0)set(handles.edit1,'String',a)jj=1;endj=0;% --- 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) global jjglobal ja=get(handles.edit1,'String')a=strcat(a,'/')if(jj==0)set(handles.edit1,'String',a)jj=1;endj=0;% --- 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) global jjglobal ja=get(handles.edit1,'String')a=strcat(a,'.')if(jj==0)set(handles.edit1,'String',a)jj=1;endj=0;% --- 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)a=get(handles.edit1,'String')b=eval(a)set(handles.edit1,'String',num2str(b^2))% --- 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)a=get(handles.edit1,'String');b=eval(a)b=b*pi/180;b=sin(b);set(handles.edit1,'String',b)% --- 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)a=get(handles.edit1,'String');b=eval(a)b=b*pi/180;b=cos(b);set(handles.edit1,'String',b)% --- 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)a=get(handles.edit1,'String');b=eval(a)if(mod(b,90)~=0)b=b*pi/180;b=tan(b);set(handles.edit1,'String',b)elseset(handles.edit1,'String','error:?¡§?')end% --- 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)a=get(handles.edit1,'String');b=eval(a)if(b~=0)b=b*pi/180;b=cot(b);set(handles.edit1,'String',b)elseset(handles.edit1,'String','error')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)set(handles.edit1,'String','0')% --- 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) global jja=get(handles.edit1,'String');if(strcmp(a,'0.')==1)&(jj==0)set(handles.edit1,'String','0.')elsess=char(a);l=length(a);a=ss(1:l-1);set(handles.edit1,'String',a)endjj=0;% --- 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) global jja=get(handles.edit1,'String')if(strcmp(a,'0')==1)&&(jj==0)set(handles.edit1,'String','(')elsea=strcat(a,'(')set(handles.edit1,'String',a)endjj=0% --- 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)a=get(handles.edit1,'String')s1=strcat(a,')')set(handles.edit1,'String',s1)% --- Executes on button press in pushbutton25.function pushbutton25_Callback(hObject, eventdata, handles)% hObject handle to pushbutton25 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)a=get(handles.edit1,'string')b=eval(a)set(handles.edit1,'string',num2str(b))。

相关文档
最新文档