MATLAB圖形界面設計(下)

文章參考Blue Mountainhtml

http://www.javashuo.com/article/p-zayovuhu-ey.htmlapp

1、菜單設計

1.創建菜單項

(1)創建一級菜單的函數調用:

  一級菜單句柄=uimenu(圖形窗口句柄,屬性名1,屬性值1,屬性名2,屬性值2,……);ide

%創建一級菜單需給出圖形窗口的句柄值。函數

%若是省略句柄值,MATLAB就在當前圖形窗口中創建這個菜單項。字體

%若是當前沒有圖形窗口,則自動打開一個圖形窗口。ui

(2)創建子菜單項的函數調用:

子菜單項句柄=uimenu(一級菜單項句柄,屬性名1,屬性值1,屬性名2,屬性值2,……);spa

2.菜單屬性值.net

(1)Label屬性:設計

     取值是字符串,用於定義菜單項的名字。能夠在字符串中加入&字符,這時在菜單項名字上,跟隨&字符有一條下劃線,&字符自己不出如今菜單項中。對於這種有帶下劃線的菜單,能夠用Alt鍵加該字符鍵來激活相應的菜單項。3d

(2)Accelerator屬性:

     取值是任何字母,用於定義菜單項的快捷鍵。

(3)Callback屬性:

     能夠是某個M文件名或者一組MATLAB命令。即該命令被選中後,MATLAB會自動調用此回調函數進行相應。

(4)Checked屬性:

     取值是on或者off(缺省值),該屬性爲菜單項定義一個指示標記,能夠用這個特性指明菜單項是否被選中。

(5)Enable屬性:

     取值是on(缺省值)或者off,這個屬性控制菜單項的可選擇性。若是是off,則此時菜單項不可用,呈現灰色。

(6)Position屬性:

     取值是數值,定義一級菜單項在菜單條上的相對位置或者紫菜單項子菜單組內的相對位置。

(7)Separator屬性:

     取值是on或者off(缺省值)。若是該屬性爲on,則在該菜單項上方添加一條分隔線,能夠用來分隔同一菜單內的不一樣的功能。

3.實例

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','圖形演示系統','NumberTitle','off','MenuBar','none'); %定義plot菜單項 hplot=uimenu(gcf,'Label','&Plot'); uimenu(hplot,'Label','Sine Wave','Callback',['t=-pi:pi/20:pi;','plot(t,sin(t));','set(hgon,''Enable'',''on'');','set(hgoff,''Enable'',''on'');','set(hbon,''Enable'',''on'');','set(hboff,''Enab;e'',''on'');']);uimenu(hplot,'Label','Cosine Wave','Callback',['t=-pi:pi/20:pi;','plot(t,cos(t));','set(hgon,''Enable'',''on'');','set(hgoff,''Enable'',''on'');','set(hbon,''Enable'',''on'');','set(hboff,''Enab;e'',''on'');']); %定義Option菜單項 hoption=uimenu(gcf,'Label','&Option'); hgon=uimenu(hoption,'Label','&Grid on','Callback','grid on','Enable','off'); hgoff=uimenu(hoption,'Label','&Grid off','Callback','grid off','Enable','off'); hbon=uimenu(hoption,'Label','&Box on','separator','on','Callback','box on','Enable','off'); hboff=uimenu(hoption,'Label','&Box off','separator','off','Callback','box off','Enable','off'); hwincor=uimenu(hoption,'Label','&Window Color','Separator','on'); %定義window coloruimenu(hwincor,'Label','&Red','Accelerator','r','Callback','set(gcf,''Color'',''r'');'); uimenu(hwincor,'Label','&Blue','Accelerator','b','Callback','set(gcf,''Color'',''b'');'); uimenu(hwincor,'Label','&Yellow','Accelerator','y','Callback','set(gcf,''Color'',''y'');'); uimenu(hwincor,'Label','&White','Accelerator','w','Callback','set(gcf,''Color'',''w'');'); %定義Quit菜單項 uimenu(gcf,'Label','&Quit','Callback','close(gcf)');

4.快捷菜單

(1)使用uicontextmenu函數創建快捷菜單

(2)使用uimenu函數爲快捷菜單創建菜單項

(3)利用set函數將快捷菜單和某圖形對象聯繫起來

x=0:pi/100:2*pi; y=2*sin(5*x).*sin(x); h1=plot(x,y); %創建快捷菜單 hc=uicontextmenu; %創建菜單項 hls=uimenu(hc,'Label','線型'); hlw=uimenu(hc,'Label','線寬'); %設置菜單項子菜單和回調函數 uimenu(hls,'Label','虛線','Callback','set(h1,''LineStyle'','':'');'); uimenu(hls,'Label','實線','Callback','set(h1,''LineStyle'',''-'');'); uimenu(hlw,'Label','加寬','Callback','set(h1,''LineWidth'',get(h1,''LineWidth'')+1);'); uimenu(hlw,'Label','變細','Callback','set(h1,''LineWidth'',get(h1,''LineWidth'')-1);'); %將快捷菜單加入到曲線對象 set(h1,'UIContextMenu',hc);

2、對話框設計

1.對話框控件

(1)按鈕(Push Button):

    上面有文字說明,並且單擊鬆開後即觸發。

(2)雙位按鈕(Toggle Button):

    上面有文字說明,可是有2種狀態(按下和未按下)。

(3)單選按鈕(Radio Button):

     一個圈圈加上一個文字說明。是一種選擇性的按鈕。

(4)複選框(Check Box):

     一個小方框加上一個文字說明,複選框和單選按鈕類似,只不過能選擇多個選項。

(5)列表框(List Box):

     列出可供選擇的選項。

(6)彈出框(Popup Box):

     平時只顯示當前選項,單擊其右端的向下箭頭即彈出一個列表框,列出所有的選項。

(7)編輯框(Edit Box):

     可供用戶輸入數據用,在編輯框內可提供缺省的輸入值,隨後用戶能夠修改。

(8)滑動條(Slider):

     滑動條能夠用圖示的方法輸入指定範圍內的一個數量值。可使用遊標來改變數量。

(9)靜態文本:

     不能修改的提示性的文本

(10)邊框:

     修飾用戶界面。

2.對話款的設計

(1)創建控件對象:

對象句柄=uicontrol(圖形窗口句柄,屬性名1,屬性值1,屬性名2,屬屬性值2,……);

(2)控件對象的屬性:

  1)Position屬性:

       取值是[n1.n2.n3.n4]

  2)Units屬性:

       取值是能夠是pixel(像素,爲缺省值)、normalized(相對單位)、inches(英寸)、centimeters(釐米)或者points(磅)。全部的單位除了相對位置,都是從左下角爲原點做圖的。

  3)Callback屬性:

       取值是字符串,是一個回調函數。

  4)String屬性:

       取值是字符串,是一段說明文字

  5)Style屬性:

       取值能夠是push(按鈕,缺省值),toggle(雙位按鈕)、radio(單選按鈕)、check(複選框)、list(列表框)、popup(彈出框)、edit(編輯框)、text(靜態文本框)、slider(滑動條)和 frame(邊框)。

  6)BackgroundColor屬性:

       取值是表明某種顏色的字符或者某個三元組。定義空間對象區域的顏色

  7)ForegroundColor屬性:

       取值是表明某種顏色的字符或者某個三元組。定義空間說明文字的顏色

  8)Max、Min屬性:

       取值都是數值。其缺省值分別爲1和0。

       當單選按鈕激活時,它的Value屬性爲Max屬性的值。當單選按鈕位於非激活狀態時,它的Value屬性爲Min屬性的值。

       當複選框激活時,它的Value屬性爲Max屬性的值。當複選框於非激活狀態時,它的Value屬性爲Min屬性的值。

       對於滑動條對象來講,Max屬性值必須必Min屬性值大。Max定義滑動條的最大值,Min定義滑動條的最小值。

       對於編輯框,若是Max-Min>1,那麼對應的編輯框接受多行字符的輸入,若是Max-Min<=1,那麼編輯框僅接受單行字符輸入。

       對於列表框,若是Max-Min>1,那麼對應的列表框容許多項選擇,若是Max-Min<=1,對應的列表框只容許單項選擇。

       另外,邊框、彈出框和靜態文本等空間對象不適用Max和Min屬性。

  9)Value屬性:

       取值能夠是向量也能夠是數值。含義與空間對象的類型息息相關。

  10)FontAngle屬性:

        取值是normalized(缺省值)、italic和oblique。這個屬性值定義空間對象標題的字形。

  11)FontName屬性:

        取值是空間對象標題等使用字體的庫名,必須系統支持的各類字體

  12)FontSize屬性:

        取值是數值,它定義控件對象標題等的字號。字號單位由FontUnits屬性來定義。

  13)FontUnits屬性;

        取值是points(磅,缺省值)、normalized(相對單位)、inches(英寸)、centimeters(釐米)或piexl(像素)。

  14)FontWeight屬性:

        定義字體的粗細。取值是normalized(缺省值)、light、demi、bold

  15)HorizontalAlignment屬性:

        取值是left、center(缺省值)或right。用來決定說明文字在水平方向上的對齊方式。

3.實例

%創建按鈕對象 pbstart=uicontrol(gcf,'Style','Push','Position',[20,20,100,25],'String','Start Plot',... 'CallBack','t=-pi:pi/20:pi;plot(t,sin(t))'); ptgrid=uicontrol(gcf,'Style','toggle','Position',[150,20,100,25],'String','Grid','Callback','grid'); %創建單選按鈕 %單擊按鈕 pbstart=uicontrol(gcf,'Style','Push','Position',[20,20,100,25],'String','Start Plot',... 'CallBack','t=-pi:pi/20:pi;plot(t,sin(t))'); %按鈕(開關) ptgrid=uicontrol(gcf,'Style','toggle','Position',[150,20,100,25],'String','Grid','Callback','grid'); %靜態文本 htxt=uicontrol(gcf,'Style','Text','string','Color Options','Position',[200,130,150,20]); %創建單選按鈕 hr=uicontrol(gcf,'Style','radio','String','Red','Position',[200,100,150,25],'Value',1,... 'CallBack',['set(hr,''Value'',1);','set(hb,''Value'',0);','set(hy,''Value'',0);','set(gcf,''Color'',''r'')']); hb=uicontrol(gcf,'Style','radio','String','Blue','Position',[200,75,150,25],'Value',0,... 'CallBack',['set(hr,''Value'',0);','set(hb,''Value'',1);','set(hy,''Value'',0);','set(gcf,''Color'',''b'')']); hy=uicontrol(gcf,'Style','radio','String','Yellow','Position',[200,50,150,25],'Value',0,... 'CallBack',['set(hr,''Value'',0);','set(hb,''Value'',0);','set(hy,''Value'',1);','set(gcf,''Color'',''y'')']); %創建複選框 htxt=uicontrol(gcf,'Style','Text','Position',[.1,.5,.25,.1],'Units','normalized','String','set Windows Properties'); hp=uicontrol(gcf,'Style','check','Position',[.1,.4,.25,.1],'Units','normalized','String','MyPosition',... 'CallBack',['set(gcf,''Position'',[10,10,300,250]);',' if get(hp,''Value'')==1','set(gcf,''Position'',[10,10,600,500]),','end']); hc=uicontrol(gcf,'Style','check','Position',[.1,.3,.25,.1],'Units','normalized','String','MyColor',... 'CallBack',['set(gcf,''color'',''w'');',' if get(hc,''Value'')==1','set(gcf,''color'',''g''),','end']); hn=uicontrol(gcf,'Style','check','Position',[.1,.2,.25,.1],'Units','normalized','String','MyName',... 'CallBack',['set(gcf,''Name'',''複選框未選中'');',' if get(hn,''Value'')==1','set(gcf,''Name'',''複選框已選中''),','end']); %創建彈出框 hpop=uicontrol(gcf,'Style','popup','Units','pixel','String','red|blue|green|yellow',...%用|隔開各個選項 'Position',[100,300,100,80],'Callback',['cbcol=[''R'',''B'',''G'',''Y''];','set(gcf,''Color'',cbcol(get(hpop,''Value'')))']);%一個比較好的設置方法 %創建列表框 hl=uicontrol(gcf,'Style','List','String','red|blue|green|yellow|white|black',... 'position',[400,100,100,80],'Callback',... ['cacol=[''r'',''b'',''g'',''y'',''w'',''k''];','set(gcf,''color'',cacol(get(hl,''Value'')));']); %創建一個編輯框,並加邊框 ftdir=uicontrol(gcf,'Style','frame','back','y','Position',[30,180,120,100]); edmulti=uicontrol(gcf,'Style','edit','string','Matlab is a very useful tool','position',[50,200,75,55],'Max',2,'back','w'); %創建滑動條 fig=figure('position',[200,200,400,300]); %繪製一個圖形窗口 hsli1=uicontrol(fig,'Style','slider','position',[50,50,120,20],'Min',200,'Max',800,'Value',400,... 'Callback',['set(azmcur,''string'',num2str(get(hsli1,''value'')));','set(gcf,''position'',[200,200,get(hsli1,''value''),get(hsli2,''value'')])']); hsli2=uicontrol(fig,'style','slider','position',[240,50,120,20],'Min',200,'max',800,'Value',400,... 'callback',['set(elvcur,''string'',num2str(get(hsli2,''value'')));','set(gcf,''position'',[200,200,get(hsli1,''value''),get(hsli2,''value'')])']); %靜態文本標出最小值 azmmin=uicontrol(fig,'Style','text','Position',[20,50,30,20],'string',num2str(get(hsli1,'min'))); elvmin=uicontrol(fig,'style','text','position',[210,50,30,20],'string',num2str(get(hsli2,'min'))); %用靜態文本標出最大值 azmmax=uicontrol(fig,'style','text','position',[170,50,30,20],'string',num2str(get(hsli1,'max'))); elvmax=uicontrol(fig,'style','text','position',[360,50,30,20],'string',num2str(get(hsli2,'max'))); %用靜態文本標出當前設置的寬度和高度 azmLabel=uicontrol(fig,'Style','text','position',[50,80,65,20],'string','width'); elvLabel=uicontrol(fig,'style','text','position',[240,80,65,20],'string','height'); azmcur=uicontrol(fig,'style','text','position',[120,80,50,20],'string',num2str(get(hsli1,'Value'))); elvcur=uicontrol(fig,'style','text','position',[310,80,50,20],'string',num2str(get(hsli2,'value')));

3、用戶界面設計

1.圖形界面設計

(1)打開GUI設計模板

(2)GUI界面

有四種模板能夠選擇,選BLANK 模板開始建立

(3)建立界面

(4)各個控件

2.實例

設計流程:

(1)在界面上繪製各控件,以下

(2)右鍵,進入CALLBACK

(3)填寫程序

繪圖程序

handles.peaks=peaks(100); handles.membrane=membrane; [x,y]=meshgrid(-8:0.5:8); r=sqrt(x.^2+y.^2); sinc=sin(r)./(r+eps); handles.sinc=sinc;

彈出菜單程序

% --- 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) mesh(handles.current_data) % --- 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) contour3(handles.current_data) % --- 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) surf(handles.current_data)

添加Select菜單項

function Untitled_2_Callback(hObject, eventdata, handles) % hObject handle to Untitled_2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(gcbf,'Color','y'); % -------------------------------------------------------------------- function Untitled_4_Callback(hObject, eventdata, handles) % hObject handle to Untitled_4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(gcbf,'Color','r');

每一個按鈕填寫程序

% --- 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) mesh(handles.current_data) % --- 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) contour3(handles.current_data) % --- 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) surf(handles.current_data)

排除錯誤

結果

相關文章
相關標籤/搜索