【Get深一度】Matlab_GUI handles、hobject、guidata、两种数据GUI data+application data解读

本文介绍了MATLAB GUI中按钮和密码框的回调函数实现细节,包括按键响应和鼠标点击事件处理,并探讨了句柄(handles)和对象句柄(hObject)在GUI中的作用。

% --- Executes on key press with focus on btnlog and none of its controls.

function XXXPressFcn(hObject, eventdata, handles)

% hObject    handle to btnlog (see GCBO)

% eventdata  structure with the following fields (see UICONTROL)

% Key: name of the key that was pressed, in lower case

% Character: character interpretation of the key(s) that was pressed

% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed

% handles    structure with handles and user data (see GUIDATA)



% - - -执行按键上的焦点btnlog和无控制功能。

函数btnlog_XXXPressFcn(hObject eventdata,处理)

% hObject句柄btnlog(见GCBO)

% 事件数据:以下字段结构(参见UICONTROL)

% 关键字:  名称键被按下,在较低的情况下

% 字符:    字符解释的关键(s)

% 修饰符:  被按关键修饰符的名称(s)(如,、控制、转移)

% 处理:    处理结构和用户数据(见GUIDATA)


% --- If Enable == 'on', executes on mouse press in 5 pixel border.

% --- Otherwise, executes on mouse press in 5 pixel border or over password.

function password_ButtonDownFcn(hObject, eventdata, handles)

% hObject    handle to password (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)


% - - - - - -如果启用= =‘上’,上执行鼠标按5像素边界。

% - - -否则,鼠标按上执行在5像素边界或密码。

函数password_ButtonDownFcn(hObject eventdata,处理)

% hObject:处理密码(见GCBO)

% eventdata:保留——MATLAB的未来版本中定义

% 处理:结构处理和用户数据(见GUIDATA)



h:        指回调函数被调用对象的句柄;
handles:  包含GUI中所有组件句柄的结构体,该结构体的域名由对象的TAG属性定义。也可以用来传递数据给其他的回调函数和主程序。
例:
  • 创建一个包含button的GUI,button的TAG属性设为pushbutton1;
  • GUIDE在应用程序M文件中生成如下的回调子函数:                                                                             function pushbutton1_callback(h,evendata,handles,varargin)
  • 然后设置button的callback      mygui('pushbutton1_callback',gcbo,[],guidata(gcbo))
其中:
mygui:                FIG文件名
ppushbutton1_callback:回调子函数名
gcbo:                 返回按钮句柄
[]:                   空矩阵
guidata(gcbo):        从图形窗口的应用程序数据中获得的句柄结构体

形象寓意:如果把figure对象当做一个大缸,而把其他对象例如AXES,BUTTON等等当做这个大缸里的小缸,那么handles就相当于这个大缸的句柄,可以用大缸的句柄来设置小缸的属性,例handles.pushbutton。
hobject就相当于每一个小缸的句柄了,可以直接在响应控件的function后面加

set(hObject,'property','value');

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在MATLAB GUI中,数据分两种:GUI data和application data。 两种数据的存取机制是相同的,但是GUI data使用起来比较方便。

  • 每一个GUI 图形界面都维持一个和自己的界面的图形(figure)相联系的一个handles数据结构,这个数据结构中容纳图形界面内所有控件(按钮、列表框、编辑框等)的句柄,相当于一个大的“容器”,里面存放了figure内所有控件的句柄。
  • 同时,handles结构也可以被figure内所有控件的回调函数访问,因为回调函数的输入参数中都有handles结构。此外,在控件的回调函数内可以把数据存储到handles结构中。
  • guidata函数:取得和存储handles结构数据

   例如:在编辑框edit的回调函数内想获得t编辑框的句柄,hObject可以,也可用handles.edit,这两个值是一样的,没有区别,只不过获得控件句柄的方式不同而已

  • hObject是调用回调函数时直接传过来的
  • handles.edit是从handles结构中取得的。
  • 但是,在控件的CreateFcn函数中如果想访问控件,必须用hObject,而不能用handles.edit,因为这时控件还没被创建,其句柄还没有加入到handles结构中。
  • 各控件的回调函数中,hObject的值是不一样的,分别代表调用回调函数的控件的句柄,而handles结构却是一样的。这种机制便于figure内的不同控件的回调函数内传递数据。


请给一个代码实现以上功能function varargout = D2(varargin) % D2 MATLAB code for D2.fig % D2, by itself, creates a new D2 or raises the existing % singleton*. % % H = D2 returns the handle to a new D2 or the handle to % the existing singleton*. % % D2('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in D2.M with the given input arguments. % % D2('Property','Value',...) creates a new D2 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before D2_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to D2_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 D2 % Last Modified by GUIDE v2.5 06-Nov-2025 16:27:08 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @D2_OpeningFcn, ... 'gui_OutputFcn', @D2_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 D2 is made visible. function D2_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 D2 (see VARARGIN) % Choose default command line output for D2 handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes D2 wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = D2_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 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) % -------------------------------------------------------------------- function Untitled_1_Callback(hObject, eventdata, handles) % hObject handle to Untitled_1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_2_Callback(hObject, eventdata, handles) % hObject handle to Untitled_2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) % hObject handle to pushbutton3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) 在这个代码上进行添加
最新发布
11-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值