12/31/2006

範例一:執行各繪圖

利用GUI介面執行程式,最簡單的方式是使用GUIDE之輔助畫面,首先執行GUIDE指令如下,然後選擇空白畫面:

>>guide



將上面圖形準備妥當,並將跳出式選單中之string參數加入下列選項:


'plot(x)', 'pie(x)', 'hist(y)', 'ezplot3(f)',...
'polar(theta,r)','area(x,y)'

由圖面先執行,設其檔案為demo3.m,並令其自動產生該檔案,如下述:
其中黃色部份為另外添者。

function varargout = demo3(varargin)
% DEMO3 M-file for demo3.fig
% DEMO3, by itself, creates a new DEMO3 or raises the existing
% singleton*.
%
% H = DEMO3 returns the handle to a new DEMO3 or the handle to
% the existing singleton*.
%
% DEMO3('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DEMO3.M with the given input arguments.
%
% DEMO3('Property','Value',...) creates a new DEMO3 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before demo3_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to demo3_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's 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 demo3

% Last Modified by GUIDE v2.5 04-Jan-2007 10:02:38

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @demo3_OpeningFcn, ...
'gui_OutputFcn', @demo3_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before demo3 is made visible.
function demo3_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see 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 command line arguments to demo3 (see VARARGIN)

% Choose default command line output for demo3
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes demo3 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
if strcmp(get(hObject,'Visible'),'off')
plot(rand(5));
end

% --- Outputs from this function are returned to the command line.
function varargout = demo3_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 command line output from handles structure
varargout{1} = handles.output;


% --- 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)

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

set(hObject, 'String', {'plot(x)', 'pie(x)', 'hist(y)', 'ezplot3(f)',...
'polar(theta,r)','area(x,y)'});

% --- 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)
axes(handles.axes1);
cla;

popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
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(hObject, eventdata, handles)
% 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(hObject, eventdata, handles)
% 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)
printdlg(handles.figure1)


% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% 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)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
['Close ' get(handles.figure1,'Name') '...'],...
'Yes','No','Yes');
if strcmp(selection,'No')
return;
end

delete(handles.figure1)

% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% 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)



不用Guide輔助之程式寫法



function simple_guix
% SIMPLE_GUI Select a data set from the pop-up menu, then
% click one of the plot-type push buttons. Clicking the button
% plots the selected data in the axes.

% Create and then hide the GUI as it is being constructed.
f = figure('Visible','off','Position',[360,500,450,285]);

% Construct the components.
h1 = uicontrol('Style','pushbutton','String','開始',...
'Position',[315,220,70,25],...
'Callback',{@startbutton_Callback});
htext = uicontrol('Style','text','String','請選擇繪圖指令:',...
'Position',[325,90,60,15]);
hpopup = uicontrol('Style','popupmenu',...
'String',{'plot(x)', 'pie(x)', 'hist(y)', 'ezplot3(f)',...
'polar(theta,r)','area(x,y)'},...
'Position',[300,50,100,25],...
'Callback',{@popup_menu_Callback});
ha = axes('Units','Pixels','Position',[50,60,200,185]);

% Initialize the GUI.
% Change units to normalized so components resize
% automatically.
set([f,ha,h1,htext,hpopup],'Units','normalized');
%Create a first plot in the axes.
plot(rand(5));
currentfig='plot(x)';
% Assign the GUI a name to appear in the window title.
set(f,'Name','Simple GUI')
% Move the GUI to the center of the screen.
movegui(f,'center')
% Make the GUI visible.
set(f,'Visible','on');

% Callbacks for simple_gui.
% the current data.
function popup_menu_Callback(source,eventdata)
% Determine the selected data set.
str = get(source, 'String');
val = get(source,'Value');
currentfig=cell2mat(str(val));
end

% Push button callbacks. Each callback plots current_data in
% the specified plot type.

function startbutton_Callback(source,eventdata)
% Display surf plot of the currently selected data.
% Set current data to the selected data set.
switch currentfig;
case 'plot(x)' % User selects plot.
plot(rand(5));
case 'pie(x)' % User selects pie.
n=round(10*rand)+10;
pie(2:4:n);
case 'hist(y)' % User selects hist
hist(randn(1000,1));
case 'ezplot3(f)' % User selects ezplot
ezplot3('t*sin(t)', 'cos(t)', 't', [0,6*pi])
case 'polar(theta,r)' % User selects polar
theta = linspace(0, 2*pi);
polar(theta, 3+2*rand*cos(4*theta));
case 'area(x,y)' % User selects area
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

end


end

沒有留言:

張貼留言