第08章-MATLAB圖形用戶界面設(shè)計(jì)-例題源程序_第1頁(yè)
第08章-MATLAB圖形用戶界面設(shè)計(jì)-例題源程序_第2頁(yè)
第08章-MATLAB圖形用戶界面設(shè)計(jì)-例題源程序_第3頁(yè)
第08章-MATLAB圖形用戶界面設(shè)計(jì)-例題源程序_第4頁(yè)
第08章-MATLAB圖形用戶界面設(shè)計(jì)-例題源程序_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

高教社劉衛(wèi)國(guó)《MATLAB程序設(shè)計(jì)與應(yīng)用》(第二版)例題源程序第8章:MATLAB圖形用戶界面設(shè)計(jì)PAGEPAGE1王成優(yōu)?山東大學(xué)(威海)MATLAB語(yǔ)言WangChengyou?ShandongUniversity,Weihai第8章MATLAB圖形用戶界面設(shè)計(jì)例8.1建立圖8.1所示的圖形演示系統(tǒng)菜單。菜單條中含有3個(gè)菜單項(xiàng):Plot、Option和Quit。Plot中有SineWave和CosineWave兩個(gè)子菜單項(xiàng),分別控制在本圖形窗口畫(huà)出正弦和余弦曲線。Option菜單項(xiàng)的內(nèi)容如圖8.1所示,其中Gridon和Gridoff控制給坐標(biāo)軸加網(wǎng)格線,Boxon和Boxoff控制給坐標(biāo)軸加邊框,而且這4項(xiàng)只有在畫(huà)有曲線時(shí)才是可選的。WindowColor控制圖形窗口背景顏色。Quit控制是否退出系統(tǒng)。screen=get(0,'ScreenSize');W=screen(3);H=screen(4);figure('Color',[1,1,1],'Position',[0.2*H,0.2*H,0.5*W,0.3*H],...'Name','圖形演示系統(tǒng)','NumberTitle','off','MenuBar','none');%定義Plot菜單項(xiàng)hplot=uimenu(gcf,'Label','&Plot');uimenu(hplot,'Label','SineWave','Call',...['t=-pi:pi/20:pi;','plot(t,sin(t));',...'set(hgon,''Enable'',''on'');',...'set(hgoff,''Enable'',''on'');',...'set(hbon,''Enable'',''on'');',...'set(hboff,''Enable'',''on'');']);uimenu(hplot,'Label','CosineWave','Call',...['t=-pi:pi/20:pi;','plot(t,cos(t));',...'set(hgon,''Enable'',''on'');',...'set(hgoff,''Enable'',''on'');',...'set(hbon,''Enable'',''on'');',...'set(hboff,''Enable'',''on'');']);%定義Option菜單項(xiàng)hoption=uimenu(gcf,'Label','&Option');hgon=uimenu(hoption,'Label','&Gridon',...'Call','gridon','Enable','off');hgoff=uimenu(hoption,'Label','&Gridoff',...'Call','gridoff','Enable','off');hbon=uimenu(hoption,'Label','&Boxon',...'separator','on','Call','boxon','Enable','off');hboff=uimenu(hoption,'Label','&Boxoff',...'Call','boxoff','Enable','off');hwincor=uimenu(hoption,'Label','&WindowColor','Separator','on');uimenu(hwincor,'Label','&Red','Accelerator','r',...'Call','set(gcf,''Color'',''r'');');uimenu(hwincor,'Label','&Blue','Accelerator','b',...'Call','set(gcf,''Color'',''b'');');uimenu(hwincor,'Label','&Yellow','Call',...'set(gcf,''Color'',''y'');');uimenu(hwincor,'Label','&White','Call',...'set(gcf,''Color'',''w'');');%定義Quit菜單項(xiàng)uimenu(gcf,'Label','&Quit','Call','close(gcf)');例8.2繪制曲線y=2e-0.5xsin(2πx),并建立一個(gè)與之相聯(lián)系的快捷菜單,用以控制曲線的線型和曲線寬度。x=0:pi/100:2*pi;y=2*exp(-0.5*x).*sin(2*pi*x);hl=plot(x,y);hc=uicontextmenu;%建立快捷菜單hls=uimenu(hc,'Label','線型');%建立菜單項(xiàng)hlw=uimenu(hc,'Label','線寬');uimenu(hls,'Label','虛線','Call','set(hl,''LineStyle'','':'');');uimenu(hls,'Label','實(shí)線','Call','set(hl,''LineStyle'',''-'');');uimenu(hlw,'Label','加寬','Call','set(hl,''LineWidth'',2);');uimenu(hlw,'Label','變細(xì)','Call','set(hl,''LineWidth'',0.5);');set(hl,'UIContextMenu',hc);%將該快捷菜單和曲線對(duì)象聯(lián)系起來(lái)例8.3建立如圖8.10所示的數(shù)制轉(zhuǎn)換對(duì)話框。在左邊輸入一個(gè)十進(jìn)制整數(shù)和2~16之間的數(shù),單擊“轉(zhuǎn)換”按鈕能在右邊得到十進(jìn)制數(shù)所對(duì)應(yīng)的2~16進(jìn)制字符串,單擊“退出”按鈕退出對(duì)話框。hf=figure('Color',[0,1,1],'Position',[100,200,400,200],...'Name','數(shù)制轉(zhuǎn)換','NumberTitle','off','MenuBar','none');uicontrol(hf,'Style','Text','Units','normalized',...'Position',[0.05,0.8,0.45,0.1],'Horizontal','center',...'String','輸入框','Back',[0,1,1]);uicontrol(hf,'Style','Text','Units','normalized',...'Position',[0.5,0.8,0.45,0.1],'Horizontal','center',...'String','輸出框','Back',[0,1,1]);uicontrol(hf,'Style','Frame','Units','normalized',...'Position',[0.04,0.33,0.45,0.45],'Back',[1,1,0]);uicontrol(hf,'Style','Text','Units','normalized',...'Position',[0.05,0.6,0.25,0.1],'Horizontal','center',...'String','十進(jìn)制數(shù)','Back',[1,1,0]);uicontrol(hf,'Style','Text','Units','normalized',...'Position',[0.05,0.4,0.25,0.1],'Horizontal','center',...'String','2~16進(jìn)制','Back',[1,1,0]);he1=uicontrol(hf,'Style','Edit','Units','normalized',...'Position',[0.25,0.6,0.2,0.1],'Back',[0,1,0]);he2=uicontrol(hf,'Style','Edit','Units','normalized',...'Position',[0.25,0.4,0.2,0.1],'Back',[0,1,0]);uicontrol(hf,'Style','Frame','Units','normalized',...'Position',[0.52,0.33,0.45,0.45],'Back',[1,1,0]);ht=uicontrol(hf,'Style','Text','Units','normalized',...'Position',[0.6,0.5,0.3,0.1],'Horizontal','center',...'Back',[0,1,0]);COMM=['n=str2num(get(he1,''String''));',...'b=str2num(get(he2,''String''));',...'dec=trdec(n,b);','set(ht,''string'',dec);'];uicontrol(hf,'Style','Push','Units','normalized',...'Position',[0.18,0.1,0.2,0.12],'String','轉(zhuǎn)換','Call',COMM);uicontrol(hf,'Style','Push','Units','normalized',...'Position',[0.65,0.1,0.2,0.12],...'String','退出','Call','close(hf)');程序調(diào)用了trdec.m函數(shù)文件,該函數(shù)的作用是將任意十進(jìn)制整數(shù)轉(zhuǎn)換為2~16進(jìn)制字符串。trdec.m函數(shù)文件如下:functiondec=trdec(n,b)ch1='0123456789ABCDEF';%十六進(jìn)制的16個(gè)符號(hào)k=1;whilen~=0%不斷除某進(jìn)制基數(shù)取余直到商為0p(k)=rem(n,b);n=fix(n/b);k=k+1;endk=k-1;strdec='';whilek>=1%形成某進(jìn)制數(shù)的字符串kb=p(k);strdec=strcat(strdec,ch1(kb+1:kb+1));k=k-1;enddec=strdec;例8.4建立如圖8.11所示的圖形演示對(duì)話框。在編輯框輸入繪圖命令,當(dāng)單擊“繪圖”按鈕時(shí),能在左邊坐標(biāo)軸繪制所對(duì)應(yīng)的圖形,彈出框提供色圖控制,列表框提供坐標(biāo)網(wǎng)格線和坐標(biāo)邊框控制。clf;set(gcf,'Unit','normalized','Position',[0.2,0.3,0.55,0.36]);set(gcf,'Menubar','none','Name','圖形演示','NumberTitle','off');axes('Position',[0.05,0.15,0.55,0.7]);uicontrol(gcf,'Style','text','Unit','normalized',...'Posi',[0.63,0.85,0.2,0.1],'String',...'輸入繪圖命令','Horizontal','center');hedit=uicontrol(gcf,'Style','edit','Unit','normalized',...'Posi',[0.63,0.15,0.2,0.68],...'Max',2);%Max取2,使Max-Min>1,從而允許多行輸入hpopup=uicontrol(gcf,'Style','popup','Unit','normalized',...'Posi',[0.85,0.8,0.15,0.15],'String',...'Spring|Summer|Autumn|Winter','Call',...'COMM(hedit,hpopup,hlist)');hlist=uicontrol(gcf,'Style','list','Unit','normalized',...'Posi',[0.85,0.55,0.15,0.25],'String',...'Gridon|Gridoff|Boxon|Boxoff','Call',...'COMM(hedit,hpopup,hlist)');hpush1=uicontrol(gcf,'Style','push','Unit','normalized',...'Posi',[0.85,0.35,0.15,0.15],'String',...'繪圖','Call','COMM(hedit,hpopup,hlist)');uicontrol(gcf,'Style','push','Unit','normalized',...'Posi',[0.85,0.15,0.15,0.15],'String',...'關(guān)閉','Call','closeall');COMM.m函數(shù)文件:functionCOMM(hedit,hpopup,hlist)com=get(hedit,'String');n1=get(hpopup,'Value');n2=get(hlist,'Value');if~isempty(com)%編輯框輸入非空時(shí)eval(com');%執(zhí)行從編輯框輸入的命令chpop={'spring','summer','autumn','winter'};chlist={'gridon','gridoff','boxon','boxoff'};colormap(eval(chpop{n1}));eval(chlist{n2});end例8.5利用GUI設(shè)計(jì)工具設(shè)計(jì)圖8.18所示的用戶界面。該界面包括一個(gè)用于顯示圖形的軸對(duì)象,顯示的圖形包括表面圖、網(wǎng)格圖或等高線圖。繪制圖形的功能通過(guò)3個(gè)命令按鈕來(lái)實(shí)現(xiàn),用戶通過(guò)單擊相應(yīng)的按鈕,即可繪制相應(yīng)圖形。繪制圖形所需要的數(shù)據(jù)通過(guò)一個(gè)彈出框來(lái)選取。在彈出框中包括3個(gè)選項(xiàng),分別對(duì)應(yīng)MATLAB的數(shù)據(jù)函數(shù)peaks、membrane(該函數(shù)產(chǎn)生MATLAB標(biāo)志)和用戶自定義的繪圖數(shù)據(jù)sinc,用戶可以通過(guò)選擇相應(yīng)的選項(xiàng)來(lái)載入相應(yīng)的繪圖數(shù)據(jù)。在圖形窗口默認(rèn)的菜單條上添加一個(gè)菜單項(xiàng)Select,Select下又有兩個(gè)子菜單項(xiàng)Yellow和Red,選中Yellow項(xiàng)時(shí),圖形窗口將變成黃色,選中Red項(xiàng)時(shí),圖形窗口將變成紅色。操作步驟如下:(1)打開(kāi)GUI設(shè)計(jì)窗口,添加有關(guān)控件對(duì)象。在MATLAB命令窗口輸入命令guide,將打開(kāi)GUI設(shè)計(jì)窗口。單擊GUI設(shè)計(jì)窗口控件工具欄中的Axes按鈕,并在圖形窗口中拖出一個(gè)矩形框,調(diào)整好大小和位置。再添加3個(gè)按鈕、一個(gè)彈出框和一個(gè)靜態(tài)文本框,并調(diào)整好大小和位置。必要時(shí)可利用位置調(diào)整工具將圖形對(duì)象對(duì)齊。(2)利用屬性編輯器,設(shè)置圖形對(duì)象的屬性。打開(kāi)屬性編輯器,當(dāng)用戶在界面設(shè)計(jì)中選擇一個(gè)對(duì)象后,在屬性編輯器將列出該對(duì)象的屬性及默認(rèn)的屬性值。利用屬性編輯器把3個(gè)按鈕的Position屬性的第3和第4個(gè)分量設(shè)為相同的值,以使3個(gè)按鈕寬和高都相等。3個(gè)按鈕的String屬性分別是說(shuō)明文字Mesh、Surf和Contour3,F(xiàn)ontSize屬性設(shè)為10。雙擊彈出框,打開(kāi)該對(duì)象的屬性設(shè)置對(duì)話框。為了設(shè)置彈出框的String屬性,單擊String屬性名后面的圖標(biāo),然后在打開(kāi)的文本編輯器中輸入3個(gè)選項(xiàng):peaks、membrane、sinc。注意,每行輸入一個(gè)選項(xiàng)。將靜態(tài)文本框的String屬性設(shè)置為ChooseDataofGraphics。(3)編寫(xiě)代碼,實(shí)現(xiàn)控件功能。為了實(shí)現(xiàn)控件的功能,需要編寫(xiě)相應(yīng)的程序代碼。如果實(shí)現(xiàn)代碼較為簡(jiǎn)單,可以直接修改控件的Callback屬性。對(duì)于較為復(fù)雜的程序代碼,最好還是編寫(xiě)M文件。右鍵單擊任一圖形對(duì)象,在彈出的快捷菜單中選擇ViewCallbacks,再選Callback子菜單,將自動(dòng)打開(kāi)一個(gè)M文件,這時(shí)可以在各控件的回調(diào)函數(shù)區(qū)輸入相應(yīng)的程序代碼。本例需要添加的代碼如下(注釋部分和函數(shù)引導(dǎo)行是系統(tǒng)M文件中已有的):在打開(kāi)的函數(shù)文件中,添加用于創(chuàng)建繪圖數(shù)據(jù)的代碼:functionex8_5_OpeningFcn(hObject,eventdata,handles,varargin)%Thisfunctionhasnooutputargs,seeOutputFcn.%hObjecthandletofigure%eventdatareserved-tobedefinedinafutureversionofMATLAB%handlesstructurewithhandlesanduserdata(seeGUIDATA)%varargincommandlineargumentstoex8_5(seeVARARGIN)handles.peaks=peaks(35);handles.membrane=membrane(5);%membrane函數(shù)產(chǎn)生MATLAB標(biāo)志[x,y]=meshgrid(-8:0.5:8);r=sqrt(x.^2+y.^2);sinc=sin(r)./(r+eps);handles.sinc=sinc;handles.current_data=handles.peaks;為彈出式菜單編寫(xiě)響應(yīng)函數(shù)代碼如下:為Mesh按鈕編寫(xiě)響應(yīng)函數(shù)代碼如下:%Executesonbuttonpressinpushbutton1.functionpushbutton1_Callback(hObject,eventdata,handles)%hObjecthandletopushbutton1(seeGCBO)%eventdatareserved-tobedefinedi

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論