收藏 分销(赏)

简易计算器 matlab.doc

上传人:xrp****65 文档编号:7582736 上传时间:2025-01-09 格式:DOC 页数:14 大小:128.50KB
下载 相关 举报
简易计算器 matlab.doc_第1页
第1页 / 共14页
简易计算器 matlab.doc_第2页
第2页 / 共14页
简易计算器 matlab.doc_第3页
第3页 / 共14页
简易计算器 matlab.doc_第4页
第4页 / 共14页
简易计算器 matlab.doc_第5页
第5页 / 共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)global locaval;a = get(handles.pushbutton1,String);locaval=strcat(locaval,a);set(handles.text1,String,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);%运算符按钮处理“+、-、*、”范例%全局变量flagnum存储运算符标志%全局变量global1用

3、于储存第一个待处理数值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决策语句实现相应计算%需注意相应数据类型的转化function push

5、button17_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=nu

6、m2str(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实现其功能function

7、 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清除按钮函数%把全局变量locava

8、l清零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

9、);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)实验心得

10、:!、通过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 singl

11、eton*.% 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 function

12、 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:01:00%

13、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 nargi

14、n & 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, eventd

15、ata, 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

16、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 varar

17、gout = 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 output

18、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 and use

19、r 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)% hObject h

20、andle 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=locaval;gu

21、idata(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)global

22、 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 (see G

23、CBO)% 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(handles.push

25、button5,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 reserved - t

26、o 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 press in

27、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);locaval=strc

28、at(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 future vers

29、ion 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 pushbutt

30、on9_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(handles.tex

31、t1,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% handles st

32、ructure 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 pushb

33、utton11_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.text1,S

34、tring,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 a futu

35、re 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, execute

36、s 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% handles stru

37、cture 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(hObject, ev

38、entdata, 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,locaval);glo

39、bal 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 and use

40、r 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, handles)% h

41、Object 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 pushbutton1

42、6.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,String);set

43、(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 - to be d

44、efined 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+gloval2;case1=num2str(case1);case2=num2str(cas

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 应用文书 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服