matlab-GUI程序(1)



 guidata用来保存handles结构,可以做为窗口间参数的传递。

 

>> help guidata
 GUIDATA Store or retrieve application data.
    GUIDATA(H, DATA) stores the specified data in the figure's
    application data.
 
    H is a handle that identifies the figure - it can be the figure
    itself, or any object contained in the figure.
 
    DATA can be anything an application wishes to store for later
    retrieval.
 
    DATA = GUIDATA(H) returns previously stored data, or an empty
    matrix if nothing was previously stored.
 
    GUIDATA provides application authors with a convenient interface
    to a figure's application data. You can access the data from a
    callback subfunction using the component's handle, without needing
    to find the figure's handle.  You can also avoid having to create
    and maintain a hardcoded property name for the application data
    throughout your source code.  GUIDATA is particularly useful in
    conjunction with GUIHANDLES, which returns a structure containing
    handles of all the components in a GUI listed by tag.
 
    Example:
 
    Suppose an application creates a figure with handle F, containing
    a slider and an editable text uicontrol whose tags are
    'valueSlider' and 'valueEdit' respectively.  The following
    excerpts from the application's M-file illustrate the use of
    GUIDATA to access a structure containing handles returned by
    GUIHANDLES, plus additional application-specific data added during
    initialization and callbacks:
 
    ... excerpt from the GUI setup code ...
 
    f = openfig('mygui.fig');
    data = guihandles(f); % initialize it to contain handles
    data.errorString = 'Total number of mistakes: ';
    data.numberOfErrors = 0;
    guidata(f, data);  % store the structure
 
    ... excerpt from the slider's callback ...
 
    data = guidata(gcbo); % get the struct, use the handles:
    set(data.valueEdit, 'String',...
        num2str(get(data.valueSlider, 'Value')));
 
    ... excerpt from the edit's callback ...
 
    data = guidata(gcbo); % need handles, may need error info
    val = str2double(get(data.valueEdit,'String'));
    if isnumeric(val) & length(val)==1 & ...
       val >= get(data.valueSlider, 'Min') & ...
       val <= get(data.valueSlider, 'Max')
      set(data.valueSlider, 'Value', val);
    else
      % increment the error count, and display it
      data.numberOfErrors = data.numberOfErrors + 1;
      set(handles.valueEdit, 'String',...
       [ data.errorString, num2str(data.numberOfErrors) ]);
      guidata(gcbo, data); % store the changes...
    end
 
    Note that GUIDE generates callback functions to which a structure
    of handles is passed automatically as an input argument.  This
    eliminates the need to call "data = guidata(gcbo);" in callbacks
    written using GUIDE, unlike the example above.

 

以一个简单的一元二次方程求解为例

 先看看自动 生成的M代码

 

 

 

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

% Last Modified by GUIDE v2.5 04-Oct-2012 17:21:13

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @test1_OpeningFcn, ...
                   'gui_OutputFcn',  @test1_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 test1 is made visible.
function test1_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 test1 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes test1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = test1_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 button press in btn_draw.
function btn_draw_Callback(hObject, eventdata, handles)
% hObject    handle to btn_draw (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function a_num_Callback(hObject, eventdata, handles)
% hObject    handle to a_num (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of a_num as text
%        str2double(get(hObject,'String')) returns contents of a_num as a double


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

% Hint: edit 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



function b_num_Callback(hObject, eventdata, handles)
% hObject    handle to b_num (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of b_num as text
%        str2double(get(hObject,'String')) returns contents of b_num as a double


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

% Hint: edit 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



function c_num_Callback(hObject, eventdata, handles)
% hObject    handle to c_num (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of c_num as text
%        str2double(get(hObject,'String')) returns contents of c_num as a double


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

% Hint: edit 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

 

 

基本界面如下

 

 



 

 matlab-GUI程序(1)_第1张图片

 

 

 

你可能感兴趣的:(matlab)