主要的元件要有:
Static text:"請輸入繪圖指令:"
Pop-up Menu:'plot(x) | pie(x) | hist(y) | ezplot3(f) | polar(theta,r) | area(x,y)'
Pushbutton:開啟
axes:繪圖
然後最後還要有Menu bar的一個item
file:裡面含有:open close print三個元件
所以原本用guide方式設計的為請按此連結或是直接連到範例一
執行後畫面如下:
以及
open:開一個新的figure
close:關掉此figure並且會跳出視窗問是否確定要關掉
print:開啟印表機並且選擇列印這一個figure
若是以handle的方式執行的話,我將元件以structure的方式放入一個變數c
並且要將此變數定為global,如此才能在不同的callback中呼叫,並且利用
structure的方式,只要寫global c就可以了,不用在一個一個呼叫比較方便
所以主要的元件如下:
% Use uicontrol to create a GUI
%------------------------------------------------------------------------%
% Create and then hide the GUI as it is being constructed.
global c
c.f = figure('Visible','on','Position',[360,500,450,285]);
%------------------------------------------------------------------------%
% Construct the components
% component is constructed by structure of c.xxx
c.static=uicontrol('Style','text','Position',[60 230 100 20]);
c.popup=uicontrol('Style','popup','Position',[200 230 100 20]);
c.push=uicontrol('Style','pushbutton','Position',[320 230 50 20]);
c.axes=axes('Units','Pixels','Position',[30,30,400,185]);
c.filemenu=uimenu('Label','File');
c.openMenuItem=uimenu(c.filemenu,'Label','Open');
c.closeMenuItem=uimenu(c.filemenu,'Label','Close');
c.printMenuItem=uimenu(c.filemenu,'Label','Print');
% setup the UI
set(c.static,'String','請輸入繪圖指令:');
set(c.popup,'String','plot(x) | pie(x) | hist(y) | ezplot3(f) | polar(theta,r) | area(x,y)');
set(c.push,'String','開啟','callback',(@push_callback));
set(c.openMenuItem,'callback',(@OpenMenuItem_callback));
set(c.closeMenuItem,'callback',(@CloseMenuItem_callback));
set(c.printMenuItem,'callback',(@PrintMenuItem_callback));
將每一個元件都定義後,在將其特性以及相對應的callback寫好
程式碼如下:(或是直接按此連結)
function demo3_GUI
% Use uicontrol to create a GUI
%------------------------------------------------------------------------%
% Create and then hide the GUI as it is being constructed.
global c
c.f = figure('Visible','on','Position',[360,500,450,285]);
%------------------------------------------------------------------------%
% Construct the components
% component is constructed by structure of c.xxx
c.static=uicontrol('Style','text','Position',[60 230 100 20]);
c.popup=uicontrol('Style','popup','Position',[200 230 100 20]);
c.push=uicontrol('Style','pushbutton','Position',[320 230 50 20]);
c.axes=axes('Units','Pixels','Position',[30,30,400,185]);
c.filemenu=uimenu('Label','File');
c.openMenuItem=uimenu(c.filemenu,'Label','Open');
c.closeMenuItem=uimenu(c.filemenu,'Label','Close');
c.printMenuItem=uimenu(c.filemenu,'Label','Print');
% setup the UI
set(c.static,'String','請輸入繪圖指令:');
set(c.popup,'String','plot(x) | pie(x) | hist(y) | ezplot3(f) | polar(theta,r) | area(x,y)');
set(c.push,'String','開啟','callback',(@push_callback));
set(c.openMenuItem,'callback',(@OpenMenuItem_callback));
set(c.closeMenuItem,'callback',(@CloseMenuItem_callback));
set(c.printMenuItem,'callback',(@PrintMenuItem_callback));
%------------------------------------------------------------------------%
% Initialize the GUI.
% Change units to normalized so components resize
% automatically.
set([c.f,c.static,c.popup,c.push],'Units','normalized');
%Create a plot in the axes.
c.axes=plot(rand(5));
% Assign the GUI a name to appear in the window title.
set(c.f,'Name','demo3 GUI')
% Move the GUI to the center of the screen.
movegui(c.f,'center')
%------------------------------------------------------------------------%
% Setup the callback function of all compoment
%----------------------------------%
%---------popup_callback-----------%
%----------------------------------%
function push_callback(source,eventdata)
global c
k=get(c.popup,'value');
switch k
case 1
plot(rand(5));
case 2
n=round(10*rand)+10;
pie(2:4:n);
case 3
hist(randn(1000,1));
case 4
ezplot3('t*sin(t)', 'cos(t)', 't', [0,6*pi])
case 5
theta = linspace(0, 2*pi);
polar(theta, 3+2*rand*cos(4*theta));
case 6
y=[1 1.2 1.5 2*rand;4 4.5 6.6 7*rand;5 6.5 8 15*rand]';
area([1980 1990 2000 2008],y);
grid on;colormap cool;
end
% --------------------------------------------------------------------
function OpenMenuItem_callback(source, eventdata)
% hObject handle to OpenMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
open(file);
end
% --------------------------------------------------------------------
function PrintMenuItem_callback(source, eventdata)
% hObject handle to PrintMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global c
printdlg(c.f)
% --------------------------------------------------------------------
function CloseMenuItem_callback(source, eventdata)
% hObject handle to CloseMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global c
selection = questdlg(['Close ' get(c.f,'Name') '?'],...
['Close ' get(c.f,'Name') '...'],...
'Yes','No','Yes');
if strcmp(selection,'No')
return;
end
delete(c.f)
% --------------------------------------------------------------------
function FileMenu_Callback(source, eventdata)
% hObject handle to FileMenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
執行結果如下:
以及