GUI界面设计1 三角函数

来源:互联网 发布:echart json 地图 编辑:程序博客网 时间:2024/06/06 18:24

下面拿三角函数做一个简单的例子,说明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










0 0