matlab简易计算器Word版.doc

上传人:doc321 文档编号:14925939 上传时间:2022-02-24 格式:DOC 页数:14 大小:100KB
返回 下载 相关 举报
matlab简易计算器Word版.doc_第1页
第1页 / 共14页
matlab简易计算器Word版.doc_第2页
第2页 / 共14页
matlab简易计算器Word版.doc_第3页
第3页 / 共14页
matlab简易计算器Word版.doc_第4页
第4页 / 共14页
matlab简易计算器Word版.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《matlab简易计算器Word版.doc》由会员分享,可在线阅读,更多相关《matlab简易计算器Word版.doc(14页珍藏版)》请在三一文库上搜索。

1、电子信息工程系实验报告成 绩:课程名称:MATLAB语言 指导教师(签名):实验项目名称:MATLAB图形用户界面设计 实验时间:2010-5-8 班级: 姓名: 学号: 实验目的: 1、熟悉MATLAB的主要控件使用方法。2、熟悉MATLAB的GUI设计流程。实验环境:硬件配置( Pentium(R) 4 CPU 2.40GHz,1GB内存)操作系统(Windows XP) 编程软件:MATLAB7.0 实验内容与结果: 使用MATLAB的GUI接口设计一个简单的计算器。 效果图: 程序源码部分函数及相关注释:%09数字键及小数点按钮代码范例%全局变量locaval用于存储用户输入的多位数值

2、%全局变量gloval2用于存储待处理的第二位数值function pushbutton1_Callback(hObject, eventdata, handles)1 / 14global locaval;a = get(handles.pushbutton1,String);locaval=strcat(locaval,a);set(handles.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);%运算符按钮处理“+、-、*、”范例%全局变量flagnum存储运算符标志%全局变量gl

3、obal1用于储存第一个待处理数值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);%取相反数按钮“+-”代码%算法实现:用零减去文本框现在的值,

4、再赋值给文本框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决策语句实现相应计算%需注意相应数据类型的转化functio

5、n 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);ca

6、se3=num2str(case3);case4=num2str(case4);switch flagnum; case 1 set(handles.text1,String,case1); case 2 set(handles.text1,String,case2); case 3 set(handles.text1,String,case3); case 4 set(handles.text1,String,case4);endguidata(hObject,handles)% BackSpace按钮函数%算法实现:MATLAB是用矩阵存储数据的,相应的可以取文本框的前N-1实现其功能fu

7、nction pushbutton19_Callback(hObject, eventdata, handles)textString = get(handles.text1,String);if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ;else ss=char(textString); l=length(textString); textString=ss(1:l-1);set(handles.text1,String,textString)endguidata(hObject,handles)%C清除按钮函数%把全局变量

8、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(text

9、String);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,handle

10、s)实验心得:!、通过MATLAB简单计算器的设计,初步了解了关于MATLAB图形用户界面的部分控件的使用方法。2、 MATLAB的GUI提供的很多实用的控件,方便用于设计属于自己的图形界面。源码:function varargout = bt0(varargin)% BT0 M-file for bt0.fig% BT0, by itself, creates a new BT0 or raises the existing% singleton*.% H = BT0 returns the handle to a new BT0 or the handle to% the existing

11、 singleton*.% BT0(Property,Value,.) creates a new BT0 using the% given property value pairs. Unrecognized properties are passed via% varargin to bt0_OpeningFcn. This calling syntax produces a% warning when there is an existing singleton*.% BT0(CALLBACK) and BT0(CALLBACK,hObject,.) call the% local fu

12、nction named CALLBACK in BT0.M with the given input% arguments.% *See GUI Options on GUIDEs 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 bt0% Last Modified by GUIDE v2.5 04-May-2010 14:0

13、1:00% Begin initialization code - DO NOT EDITglobal gloval1;global gloval2;global flagnum;global locaval;gui_Singleton = 1;gui_State = struct(gui_Name, mfilename, . gui_Singleton, gui_Singleton, . gui_OpeningFcn, bt0_OpeningFcn, . gui_OutputFcn, bt0_OutputFcn, . gui_LayoutFcn, , . gui_Callback, );if

14、 nargin & isstr(varargin1) gui_State.gui_Callback = str2func(varargin1);endif nargout varargout1:nargout = gui_mainfcn(gui_State, varargin:);else gui_mainfcn(gui_State, varargin:);end% End initialization code - DO NOT EDIT% - Executes just before bt0 is made visible.function bt0_OpeningFcn(hObject,

15、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% co

16、mmand line (see VARARGIN)% Choose default command line output for bt0handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes bt0 wait for user response (see UIRESUME)% uiwait(handles.figure1);% - Outputs from this function are returned to the command line.function

17、 varargout = bt0_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 o

18、utput from handles structurevarargout1 = 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 a

19、nd user data (see GUIDATA)global locaval;a = get(handles.pushbutton1,String);locaval=strcat(locaval,a);set(handles.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hOb

20、ject 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 locaval;a = get(handles.pushbutton2,String);locaval=strcat(locaval,a);set(handles.text1,String,locaval);global gloval2gloval2=loca

21、val;guidata(hObject, handles);% - 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)

22、global locavala = get(handles.pushbutton3,String);locaval=strcat(locaval,a);set(handles.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton4.function pushbutton4_Callback(hObject, eventdata, handles)% hObject handle to pushbutton4

23、(see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global locavala = get(handles.pushbutton4,String);locaval=strcat(locaval,a);set(handles.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles

24、);% - 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 locavala = get(handle

25、s.pushbutton5,String);locaval=strcat(locaval,a);set(handles.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton6.function pushbutton6_Callback(hObject, eventdata, handles)% hObject handle to pushbutton6 (see GCBO)% eventdata reserv

26、ed - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global locavala = get(handles.pushbutton6,String);locaval=strcat(locaval,a);set(handles.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button pre

27、ss 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 locavala = get(handles.pushbutton7,String);locava

28、l=strcat(locaval,a)set(handles.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - 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 futur

29、e version of MATLAB% handles structure with handles and user data (see GUIDATA)global locavala = get(handles.pushbutton8,String);locaval=strcat(locaval,a);set(handles.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton9.function pu

30、shbutton9_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 locavala = get(handles.pushbutton9,String);locaval=strcat(locaval,a);set(handl

31、es.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - 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% hand

32、les structure with handles and user data (see GUIDATA)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);% - Executes on button press in pushbutton11.function

33、 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)a = get(handles.pushbutton11,String);b = get(handles.text1,String);set(handles.t

34、ext1,String,a);global flagnumglobal gloval1global locavallocaval= ;flagnum=2;gloval1=b;guidata(hObject, handles);% - Executes on button press in pushbutton12.function pushbutton12_Callback(hObject, eventdata, handles)% hObject handle to pushbutton12 (see GCBO)% eventdata reserved - to be defined in

35、a future version of MATLAB% handles structure with handles and user data (see GUIDATA)a = get(handles.pushbutton12,String);b = get(handles.text1,String);set(handles.text1,String,a);global flagnumglobal gloval1global locavallocaval= ;flagnum=3;gloval1=b;guidata(hObject, handles);% - If Enable = on, e

36、xecutes on mouse press in 5 pixel border.% - Otherwise, executes on mouse press in 5 pixel border or over pushbutton13.function pushbutton13_ButtonDownFcn(hObject, eventdata, handles)% hObject handle to pushbutton13 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handle

37、s structure with handles and user data (see GUIDATA)global locavala = get(handles.pushbutton13,String);locaval=strcat(locaval,a);set(handles.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton13.function pushbutton13_Callback(hObje

38、ct, 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 locavala = get(handles.pushbutton13,String);locaval=strcat(locaval,a);set(handles.text1,String,locava

39、l);global gloval2gloval2=a;guidata(hObject, handles);% - 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 a

40、nd user data (see GUIDATA)global locaval;locaval=str2num(locaval);locaval=0-locaval;locaval=num2str(locaval);set(handles.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton15.function pushbutton15_Callback(hObject, eventdata, handl

41、es)% 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)a = get(handles.pushbutton15,String);set(handles.text1,String,a);guidata(hObject, handles);% - Executes on button press in pushb

42、utton16.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.pushbutton16,String);b = get(handles.text1,Strin

43、g);set(handles.text1,String,a);global flagnumglobal gloval1global locavallocaval= ;flagnum=4;gloval1=b;guidata(hObject, handles);% - Executes on button press in pushbutton17.function pushbutton17_Callback(hObject, eventdata, handles)% hObject handle to pushbutton17 (see GCBO)% eventdata reserved - t

44、o be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global flagnumglobal gloval1global gloval2global locavallocaval= ;gloval1=str2num(gloval1);gloval2=str2num(gloval2);case1=gloval1/gloval2;case2=gloval1*gloval2;case3=gloval1-gloval2;case4=gloval1+gl

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1