下面拿三角函數做一個簡單的例子,說明GUI界面控件及回調函數的使用
第一步:首先建立一個GUI,在command命令窗口中鍵入guide,會顯示出GUI建立對話框。
第二步:把界面所需要的控件添加上去,自己設置tag值或String值。
一個axes控件,4個pushbutton控件
雙擊控件來設置控件的一些屬性,常用的屬性 tag和String
axes按鈕用來顯示三角函數的曲線,第一個pushbutton按鈕tag屬性我這裏改爲了sin,String屬性也改爲了sin,同理,後兩個pushbutton控件tag值分別爲cos、tan.還添加了一個清除控件Clear,用來清除掉axes1上顯示出來的曲線。
第三步:這是最重要的一步,回調函數callback如何寫,使這些控件各自完成自己的工作
GUI會自動生成 .m文件,在對控件進行回調時(右擊控件→View Callbacks→Callback)會自動生成 .m文件。
出現保存對話框,要對GUI進行保存,保存後會自動生成 .m文件,或單擊GUI界面 M-File Editor,會顯示出生成的 .m文件。
接下來就是在各個pushbutton下(sin、cos、tan、Clear)回調函數下添加實現功能的代碼。
.m文件中下列代碼不得改動,是每個GUI建立都會自動生成的,若改動就會出錯
function varargout = sanjiaohanshui(varargin)
% SANJIAOHANSHUI M-file for sanjiaohanshui.fig
% SANJIAOHANSHUI, by itself, creates a new SANJIAOHANSHUI or raises the existing
% singleton*.
%
% H = SANJIAOHANSHUI returns the handle to a new SANJIAOHANSHUI or the handle to
% the existing singleton*.
%
% SANJIAOHANSHUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SANJIAOHANSHUI.M with the given input arguments.
%
% SANJIAOHANSHUI('Property','Value',...) creates a new SANJIAOHANSHUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before sanjiaohanshui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to sanjiaohanshui_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 sanjiaohanshui
% Last Modified by GUIDE v2.5 25-Oct-2015 22:01:14
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @sanjiaohanshui_OpeningFcn, ...
'gui_OutputFcn', @sanjiaohanshui_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
首先在下列代碼中添加x的範圍
function sanjiaohanshui_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 sanjiaohanshui (see VARARGIN)
% Choose default command line output for sanjiaohanshui
handles.output = hObject;
%添加函數範圍
handles.x=-pi:0.01:pi;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes sanjiaohanshui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
接下來,在sin控件的回調函數下添加代碼,實現其功能(在座標軸axes1上顯示正弦曲線)
function sin_Callback(hObject, eventdata, handles)
% hObject handle to sin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x=handles.x;
y=sin(x);
plot(handles.axes1,x,y);
同理,在cos、tan、Clear回調函數下添加如下代碼
function cos_Callback(hObject, eventdata, handles)
% hObject handle to cos (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x=handles.x;
y=cos(x);
plot(handles.axes1,x,y);
function tan_Callback(hObject, eventdata, handles)
% hObject handle to tan (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x=handles.x;
y=tan(x);
plot(handles.axes1,x,y);
function clear_Callback(hObject, eventdata, handles)
% hObject handle to clear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%
try
delete(allchild(handles.axes1)); %%清除座標軸上的三角函數曲線
end