用戶菜單通常含有一級菜單和二級菜單,乃至多級菜單。每一級菜單又包含多個菜單項。創建菜單可使用uimenu函數。編輯器
%創建一級菜單的函數調用: 一級菜單句柄=uimenu(圖形窗口句柄,屬性名1,屬性值1,屬性名2,屬性值2,……); %創建一級菜單需給出圖形窗口的句柄值。 %若是省略句柄值,MATLAB就在當前圖形窗口中創建這個菜單項。 %若是當前沒有圖形窗口,則自動打開一個圖形窗口。 %創建子菜單項的函數調用: 子菜單項句柄=uimenu(一級菜單項句柄,屬性名1,屬性值1,屬性名2,屬性值2,……);
一、Label屬性:ide
取值是字符串,用於定義菜單項的名字。能夠在字符串中加入&字符,這時在菜單項名字上,跟隨&字符有一條下劃線,&字符自己不出如今菜單項中。對於這種有帶下劃線的菜單,能夠用Alt鍵加該字符鍵來激活相應的菜單項。函數
二、Accelerator屬性:工具
取值是任何字母,用於定義菜單項的快捷鍵。字體
三、Callback屬性:ui
能夠是某個M文件名或者一組MATLAB命令。即該命令被選中後,MATLAB會自動調用此回調函數進行相應。spa
四、Checked屬性:設計
取值是on或者off(缺省值),該屬性爲菜單項定義一個指示標記,能夠用這個特性指明菜單項是否被選中。3d
五、Enable屬性:code
取值是on(缺省值)或者off,這個屬性控制菜單項的可選擇性。若是是off,則此時菜單項不可用,呈現灰色。
六、Position屬性:
取值是數值,定義一級菜單項在菜單條上的相對位置或者紫菜單項子菜單組內的相對位置。
七、Separator屬性:
取值是on或者off(缺省值)。若是該屬性爲on,則在該菜單項上方添加一條分隔線,能夠用來分隔同一菜單內的不一樣的功能。
一個demo:
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 color uimenu(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)');
快捷菜單是用鼠標右鍵單擊某對象時在屏幕彈出的菜單。
(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);
一、按鈕(Push Button):
上面有文字說明,並且單擊鬆開後即觸發。
二、雙位按鈕(Toggle Button):
上面有文字說明,可是有2種狀態(按下和未按下)。
三、單選按鈕(Radio Button):
一個圈圈加上一個文字說明。是一種選擇性的按鈕。
四、複選框(Check Box):
一個小方框加上一個文字說明,複選框和單選按鈕類似,只不過能選擇多個選項。
五、列表框(List Box):
列出可供選擇的選項。
六、彈出框(Popup Box):
平時只顯示當前選項,單擊其右端的向下箭頭即彈出一個列表框,列出所有的選項。
七、編輯框(Edit Box):
可供用戶輸入數據用,在編輯框內可提供缺省的輸入值,隨後用戶能夠修改。
八、滑動條(Slider):
滑動條能夠用圖示的方法輸入指定範圍內的一個數量值。可使用遊標來改變數量。
九、靜態文本:
不能修改的提示性的文本
十、邊框:
修飾用戶界面。
對象句柄=uicontrol(圖形窗口句柄,屬性名1,屬性值1,屬性名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。用來決定說明文字在水平方向上的對齊方式。
(1)創建按鈕對象:
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');
效果展現:
(2)創建單選按鈕。
%單擊按鈕 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'')']);
實驗結果:
(3)創建複選框
%創建複選框 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']);
(4)彈出框
%創建彈出框 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'')))']);%一個比較好的設置方法
結果以下
(5)創建列表框
%創建列表框 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'')));']);
(6)創建一個編輯框,並加邊框。
當須要邊框組織空間對象時,必須在定義控件對象以前創建邊框對象,或者說邊框對象必須覆蓋改組中全部的控件對象。爲了留出邊框的邊界邊框對象所佔用的區域至少要比該組中全部空間對象佔用的區域大。
編輯框分爲多行編輯框和但行編輯框。屬性Max與屬性Min之差小於或等於1時,爲單行編輯框,不然爲多行編輯框。
%創建一個編輯框,並加邊框 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');
(7)創建兩個滑動條,設置圖形窗口的寬度和高度。
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')));
(三)圖形用戶界面設計工具
1、圖形用戶界面設計窗口
一、GUI設計模板
打開流程= =
二、GUI設計窗口
在上圖點擊空白模板建立後。
三、一個demo
每一個編輯器的做用就很少說了,畢竟是已經漢化過了,因此咱們用起來那是駕輕就熟。而後直接上個demo
要求以下
設計流程:
(1)先在GUI設計面板上畫出這些東東
(2)而後在右鍵任何一個控件,進入到CALLBACK中,就會自動生成構架代碼,咱們須要的就是在各自的相應中加入代碼就能夠了
(3)在OpeningFcn中寫入用於建立繪圖數據的代碼,在這裏是用來關聯彈出菜單的數據。
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;
(4)爲彈出菜單寫相應代碼
% --- Executes on selection change in popupmenu1. function popupmenu1_Callback(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) val=get(hObject,'Value') str=get(hObject,'string'); switch str{val} case 'peaks' handles.current_data=handles.peaks; case 'membrane' handles.current_data=handles.membrane; case 'sinc' handles.current_data=handles.sinc; end guidata(hObject,handles)
(5)爲相應按鈕添加代碼。
% --- 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)
(6)添加Seletct菜單項,先將MenuBar設置爲figure,而後再加入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');
(7)而後排除錯誤,運行得