版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、 15/15matlab GUI 設(shè)計(簡易科學(xué)計算器) M A T L A B 大 作 業(yè) 班級: 姓名: 學(xué)號: 計算器 題目 本題目通過MATLAB的gui程序設(shè)計較為簡單,在gui設(shè)計中主要用到三種控件,文本編輯框(edit text),靜態(tài)文本框(Static text),命令按鈕(push button)。然后在通過各個按鈕的回調(diào)函數(shù),實現(xiàn)簡單的計算功能。 1、功能介紹 (1)具有友好的用戶圖形界面。實現(xiàn)十進(jìn)制數(shù)的加、減、乘、除、乘方、開方等簡單計算。 (2)具有科學(xué)計算函數(shù),包括(反)正弦、(反)余弦、(反)正切、(反)余切、開方、指數(shù)等函數(shù)運行。(注:三角函數(shù)計算的是弧度而不是
2、角度)。 (3)有清除鍵,能清除操作。 2、功能實現(xiàn) 程序由兩個部分組成:MATLAB代碼(.m文件)和GUI圖形(.fig)。 程序使用的流程:直接利用圖形界面中的按鍵鍵入所需數(shù)值、運算符等即可得出結(jié)果。 備注:軟件版本:MATLAB 2011b 首先用MATLAB GUI功能,在繪制一個靜態(tài)文本框和一個文本編輯框,以及33個命令按鈕,調(diào)整好各控件大小、顏色,整體布局如圖所示:(附錄中有相關(guān)屬性修改介紹) 然后通過雙擊各個按鈕來改寫其屬性,在m文件中編寫其回調(diào)函數(shù),最后在運行調(diào)試。 2.1 各功能界面設(shè)計 GUI設(shè)計界面:注:底部邊框用(Panel)工具添加,有兩種設(shè)計順序。(1、先加底部邊
3、框,再在底部邊框上畫功能鍵。2、先畫功能鍵,布好局,畫底框,全選功能鍵拖動到底框上。) 2.2 各功能模塊實現(xiàn)(可根據(jù)需要增減功能鍵) 算法設(shè)計: 1. 數(shù)字鍵設(shè)計:09以及小數(shù)點函數(shù)都一樣,只是參數(shù)不同: 例如:按鍵1響應(yīng): global jj textString = get(handles.text1,String); if(strcmp(textString,0.)=1) else textString =strcat(textString,1); set(handles.text1,String,textString) end jj=0; 2. 四則運算函數(shù): +功能響應(yīng): text
4、String = get(handles.text1,String); textString =strcat(textString,+); set(handles.text1,String,textString) -功能響應(yīng): textString = get(handles.text1,String); textString =strcat(textString,-); set(handles.text1,String,textString) 功能響應(yīng): textString = get(handles.text1,String); textString =strcat(textString
5、,*); set(handles.text1,String,textString) 功能響應(yīng): textString = get(handles.text1,String); textString =strcat(textString,/); set(handles.text1,String,textString) 3. 科學(xué)計算函數(shù): 例如:sin功能響應(yīng): textString = get(handles.text1,String); if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ; else a = strread(te
6、xtString, %f); a=sin(a); set(handles.text1,String,a) end 4. 退格鍵(DEL):通過取屏幕值,計算出其字符長度,然后取其前N-1項的值來實現(xiàn)退格: global jj textString = get(handles.text1,String); if(strcmp(textString,0.)=1) else ss=char(textString); l=length(textString); textString=ss(1:l-1); set(handles.text1,String,textString) end jj=0; 5.
7、 清屏鍵函數(shù)(AC): set(handles.text1,String,0.) ; 2.3 各模塊程序添加方法 選中一個需添加程序的功能鍵,右擊,View Callbacks,Callback,出現(xiàn)如下圖所示界面。(紅色框中為所需添加的程序)其他功能鍵添加方法類似。 2.4 各模塊實現(xiàn)結(jié)果 (1)數(shù)字鍵: (2)四則運算函數(shù): (3)科學(xué)計算函數(shù):Cos0的計算結(jié)果: arctan2的計算結(jié)果: 經(jīng)過計算,這些結(jié)果均與實際結(jié)果相吻合,計算器的功能實現(xiàn)的較為完好。 3、程序總結(jié): (1)小數(shù)點可以連續(xù)輸入。解決方法是:用strfind函數(shù)查看文本框里有幾個小數(shù)點,如果已經(jīng)有一個了,再按小數(shù)點就
8、保持不變。 (2)按過運算符號后一個數(shù)不等于一個數(shù),比如:輸入1,按等號,會出來一個3,經(jīng)過長時間分析得知,這是由于在按運算符號時,系統(tǒng)記錄了文本框里的數(shù)但沒有清空,才會出現(xiàn)這種問題。解決方法是再申請一個不同于加減乘除的另一個符號,并將按過運算符后記錄的數(shù)值置0。 4、心得體會: 通過本次的MATLAB課程設(shè)計,讓我對MATLAB尤其是其GUI設(shè)計的功能有了進(jìn)一步的了解,認(rèn)識到了它功能的強(qiáng)大。在MATLAB簡單計算器的設(shè)計中,了解了關(guān)于MATLAB圖形用戶界面的部分控件的使用方法;利用MATLAB的GUI提供的很多實用的控件,方便用于設(shè)計屬于自己的圖形界面和自己所需要實現(xiàn)的功能。 5、附錄:
9、(1)功能塊屬性修改表(雙擊功能塊可進(jìn)入) (2)主要程序 function varargout = untitled(varargin) %UNTITLED M-file for untitled.fig % UNTITLED, by itself, creates a new UNTITLED or raises the existing % singleton*. % % H = UNTITLED returns the handle to a new UNTITLED or the handle to % the existing singleton*. % % UNTITLED(Pr
10、operty,Value,.) creates a new UNTITLED using the % given property value pairs. Unrecognized properties are passed via % varargin to untitled_OpeningFcn. This calling syntax produces a % warning when there is an existing singleton*. % % UNTITLED(CALLBACK) and UNTITLED(CALLBACK,hObject,.) call the % l
11、ocal function named CALLBACK in UNTITLED.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 untitled % Last Modified by
12、GUIDE v2.5 19-Dec-2013 11:25:45 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct(gui_Name, mfilename, . gui_Singleton, gui_Singleton, . gui_OpeningFcn, untitled_OpeningFcn, . gui_OutputFcn, untitled_OutputFcn, . gui_LayoutFcn, , . gui_Callback, ); if nargin end if narg
13、out varargout1:nargout = gui_mainfcn(gui_State, varargin:); else gui_mainfcn(gui_State, varargin:); end % End initialization code - DO NOT EDIT % Executes just before untitled is made visible. function untitled_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see
14、 OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin unrecognized PropertyName/PropertyValue pairs from the % command line (see VARARGIN) % Choose default command line output
15、for untitled handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes untitled wait for user response (see UIRESUME) % uiwait(handles.figure1); global jj ; set(handles.text1,String,0.); jj=0; % Outputs from this function are returned to the command line. functio
16、n varargout = untitled_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default com
17、mand line output from handles structure varargout1 = 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
18、 with handles and user data (see GUIDATA) global jj textString = get(handles.text1,String); if(strcmp(textString,0.)=1) else textString =strcat(textString,1); set(handles.text1,String,textString) end jj=0; % Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, h
19、andles) % 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 jj textString = get(handles.text1,String); if(strcmp(textString,0.)=1) else textString =strcat(textString,2); set(
20、handles.text1,String,textString) end 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 dat
21、a (see GUIDATA) global jj textString = get(handles.text1,String); if(strcmp(textString,0.)=1) else textString =strcat(textString,3); set(handles.text1,String,textString) end jj=0; % Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle t
22、o pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj textString = get(handles.text1,String); if(strcmp(textString,0.)=1) else textString =strcat(textString,4); set(handles.text1,String,textS
23、tring) end jj=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 jj
24、textString = get(handles.text1,String); if(strcmp(textString,0.)=1) else textString =strcat(textString,5); set(handles.text1,String,textString) end jj=0; % Executes on button press in pushbutton6. function pushbutton6_Callback(hObject, eventdata, handles) % hObject handle to pushbutton6 (see GCBO) %
25、 eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj textString = get(handles.text1,String); if(strcmp(textString,0.)=1) else textString =strcat(textString,6); set(handles.text1,String,textString) end jj=0; % Execute
26、s 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 jj textString = get(handles.t
27、ext1,String); if(strcmp(textString,0.)=1) else textString =strcat(textString,7); set(handles.text1,String,textString) end jj=0; % Executes on button press in pushbutton8. function pushbutton8_Callback(hObject, eventdata, handles) % hObject handle to pushbutton8 (see GCBO) % eventdata reserved - to b
28、e defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj textString = get(handles.text1,String); if(strcmp(textString,0.)=1) else textString =strcat(textString,8); set(handles.text1,String,textString) end jj=0; % Executes on button press in pushb
29、utton9. 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 jj textString = get(handles.text1,String); if(strcmp(te
30、xtString,0.)=1) else textString =strcat(textString,9); set(handles.text1,String,textString) end jj=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 v
31、ersion of MATLAB % handles structure with handles and user data (see GUIDATA) global jj textString = get(handles.text1,String); if(strcmp(textString,0.)=1) else textString =strcat(textString,0); set(handles.text1,String,textString) end jj=0; % Executes on button press in pushbutton12. function pushb
32、utton12_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 jj textString = get(handles.text1,String); if(strcmp(textString,0.)=1) set(
33、handles.text1,String,0.) ; jj=1; else textString =strcat(textString,.); set(handles.text1,String,textString) end % Executes on button press in pushbutton13. function pushbutton13_Callback(hObject, eventdata, handles) % hObject handle to pushbutton13 (see GCBO) % eventdata reserved - to be defined in
34、 a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,String); textString =strcat(textString,/); set(handles.text1,String,textString) % Executes on button press in pushbutton14. function pushbutton14_Callback(hObject, eventdata, handl
35、es) % hObject handle to pushbutton14 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,String); if(strcmp(textString,0.)=1) set(handles.text1,String,() ; else textString =strcat(te
36、xtString,(); set(handles.text1,String,textString) end % Executes on button press in pushbutton16. function pushbutton16_Callback(hObject, eventdata, handles) % hObject handle to pushbutton16 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handle
37、s and user data (see GUIDATA) textString = get(handles.text1,String); if(strcmp(textString,0.)=1) set(handles.text1,String,) ; else textString =strcat(textString,); set(handles.text1,String,textString) end % Executes on button press in pushbutton17. function pushbutton17_Callback(hObject, eventdata,
38、 handles) % hObject handle to pushbutton17 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,String); textString =strcat(textString,-); set(handles.text1,String,textString) % Execu
39、tes on button press in pushbutton18. function pushbutton18_Callback(hObject, eventdata, handles) % hObject handle to pushbutton18 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,
40、String); % if(strcmp(textString,0.)=1) % set(handles.text1,String,0.) ; % else % pushbutton20 b c = strread(textString, %f %c %f); % clear ans; % % switch b % case * % ans=pushbutton20*c; % case / % if (c=0) % ans=Error Divided by zero; % else % ans=pushbutton20/c; % end % case - % ans=pushbutton20-
41、c; % case + % ans=pushbutton20+c; % % end ans =eval(textString); set(handles.text1,String,ans) % Executes on button press in pushbutton19. function pushbutton19_Callback(hObject, eventdata, handles) % hObject handle to pushbutton19 (see GCBO) % eventdata reserved - to be defined in a future version
42、of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,String); textString =strcat(textString,+); set(handles.text1,String,textString) % Executes on button press in pushbutton20. function pushbutton20_Callback(hObject, eventdata, handles) % hObject hand
43、le to pushbutton20 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,String); %strcmp(textString,0.) if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ; else a = strread(tex
44、tString, %f); a=acot(a); set(handles.text1,String,a) end % Executes on button press in pushbutton21. function pushbutton21_Callback(hObject, eventdata, handles) % hObject handle to pushbutton21 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with han
45、dles and user data (see GUIDATA) textString = get(handles.text1,String); %strcmp(textString,0.) if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ; else a = strread(textString, %f); a=sin(a); set(handles.text1,String,a) end % Executes on button press in pushbutton22. function pushbutton22_Cal
46、lback(hObject, eventdata, handles) % hObject handle to pushbutton22 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,String); %strcmp(textString,0.) if(strcmp(textString,0.)=1) se
47、t(handles.text1,String,0.) ; else a = strread(textString, %f); a=cos(a); set(handles.text1,String,a) end % Executes on button press in pushbutton23. function pushbutton23_Callback(hObject, eventdata, handles) % hObject handle to pushbutton23 (see GCBO) % eventdata reserved - to be defined in a futur
48、e version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,String); %strcmp(textString,0.) if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ; else a = strread(textString, %f); a=tan(a); set(handles.text1,String,a) end % Executes on button
49、press in pushbutton24. function pushbutton24_Callback(hObject, eventdata, handles) % hObject handle to pushbutton24 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,String); %strc
50、mp(textString,0.) if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ; else a = strread(textString, %f); a=log2(a); set(handles.text1,String,a) end % Executes on button press in pushbutton26. function pushbutton26_Callback(hObject, eventdata, handles) % hObject handle to pushbutton26 (see GCBO
51、) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,String); %strcmp(textString,0.) if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ; else a = strread(textString, %f); a=log10(a); se
52、t(handles.text1,String,a) end % Executes on button press in pushbutton27. function pushbutton27_Callback(hObject, eventdata, handles) % hObject handle to pushbutton27 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUI
53、DATA) global jj textString = get(handles.text1,String); if(strcmp(textString,0.)=1) else ss=char(textString); l=length(textString); textString=ss(1:l-1); set(handles.text1,String,textString) end jj=0; % Executes on button press in pushbutton28. function pushbutton28_Callback(hObject, eventdata, hand
54、les) % hObject handle to pushbutton28 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.text1,String,0.) ; % Executes on button press in pushbutton29. function pushbutton29_Callback(hObject, eventdat
55、a, handles) % hObject handle to pushbutton29 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,String); if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ; else a = strread(
56、textString, %f); a=asin(a); set(handles.text1,String,a) end % Executes on button press in pushbutton30. function pushbutton30_Callback(hObject, eventdata, handles) % hObject handle to pushbutton30 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with
57、handles and user data (see GUIDATA) textString = get(handles.text1,String); %strcmp(textString,0.) if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ; else a = strread(textString, %f); a=cot(a); set(handles.text1,String,a) end % Executes on button press in pushbutton31. function pushbutton31_
58、Callback(hObject, eventdata, handles) % hObject handle to pushbutton31 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,String); %strcmp(textString,0.) if(strcmp(textString,0.)=1)
59、 set(handles.text1,String,0.) ; else a = strread(textString, %f); a=a*a; set(handles.text1,String,a) end % Executes on button press in pushbutton32. function pushbutton32_Callback(hObject, eventdata, handles) % hObject handle to pushbutton32 (see GCBO) % eventdata reserved - to be defined in a futur
60、e version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,String); %strcmp(textString,0.) if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ; else a = strread(textString, %f); a=acos(a); set(handles.text1,String,a) end % Executes on button
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025版智能家居門面裝修設(shè)計與安裝服務(wù)合同范本文檔3篇
- 2025年度鋁單板產(chǎn)品系列采購合同4篇
- 二零二五年度智能飯店送餐合同協(xié)議書2篇
- 2025年度個人住宅樓頂防水維修合同范本4篇
- 2025年度臨時工勞動合同續(xù)簽與變更協(xié)議4篇
- 二零二五版“唐宋風(fēng)華”離婚協(xié)議專業(yè)咨詢與執(zhí)行合同3篇
- 2025年度個人反擔(dān)保協(xié)議樣本:跨境電商平臺融資專用4篇
- 鄭州澍青醫(yī)學(xué)高等??茖W(xué)校《公共項目管理與評估》2023-2024學(xué)年第一學(xué)期期末試卷
- 2025年度魚塘承包與漁業(yè)科技創(chuàng)新合作協(xié)議4篇
- 2025版國際貿(mào)易履約風(fēng)險控制保函擔(dān)保業(yè)務(wù)合同4篇
- 松下-GF2-相機(jī)說明書
- 產(chǎn)教融合背景下“一體兩翼三融合五重點”創(chuàng)新創(chuàng)業(yè)人才培養(yǎng)機(jī)制研究
- 新型智慧水利項目數(shù)字孿生工程解決方案
- 煤焦化焦油加工工程設(shè)計規(guī)范
- 2024年人教版小學(xué)三年級信息技術(shù)(下冊)期末試卷附答案
- 新蘇教版三年級下冊科學(xué)全冊知識點(背誦用)
- 鄉(xiāng)鎮(zhèn)風(fēng)控維穩(wěn)應(yīng)急預(yù)案演練
- 腦梗死合并癲癇病人的護(hù)理查房
- 蘇教版四年級上冊脫式計算300題及答案
- 犯罪現(xiàn)場保護(hù)培訓(xùn)課件
- 扣款通知單 采購部
評論
0/150
提交評論