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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

% --- 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内的不同控件的回调函数内传递数据。


以下是一个简单的 MATLAB GUI 界面,用于实现低照度图像增强功能。你需要创建一个名为 `gui.fig` 的 GUI 界面,并在其中添加两个滑块和一个显示图像的 axes,然后将以下代码添加到 `gui.m` 中。 ```matlab function varargout = gui(varargin) % GUI MATLAB code for gui.fig % GUI, by itself, creates a new GUI or raises the existing % singleton*. % % H = GUI returns the handle to a new GUI or the handle to % the existing singleton*. % % GUI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in GUI.M with the given input arguments. % % GUI('Property','Value',...) creates a new GUI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before gui_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to gui_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 gui % Last Modified by GUIDE v2.5 01-Nov-2021 14:09:04 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @gui_OpeningFcn, ... 'gui_OutputFcn', @gui_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 gui is made visible. function gui_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 gui (see VARARGIN) % Choose default command line output for gui handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes gui wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = gui_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 slider movement. function slider1_Callback(hObject, eventdata, handles) % hObject handle to slider1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Read the image im = imread('low_light_image.jpg'); % Get the slider value slider_value = get(handles.slider1,'Value'); % Perform the image enhancement enhanced_im = imadjust(im,[],[],slider_value); % Display the enhanced image axes(handles.axes1); imshow(enhanced_im); % Update the handles structure handles.enhanced_im = enhanced_im; guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function slider1_CreateFcn(hObject, eventdata, handles) % hObject handle to slider1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end % --- Executes on slider movement. function slider2_Callback(hObject, eventdata, handles) % hObject handle to slider2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get the slider value slider_value = get(handles.slider2,'Value'); % Perform the gamma correction gamma_corrected_im = imadjust(handles.enhanced_im,[],[],[],slider_value); % Display the gamma corrected image axes(handles.axes1); imshow(gamma_corrected_im); % Update the handles structure handles.gamma_corrected_im = gamma_corrected_im; guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function slider2_CreateFcn(hObject, eventdata, handles) % hObject handle to slider2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end ``` 在这个代码中,我们假设低照度图像的文件名为 `low_light_image.jpg`,并使用两个滑块分别进行图像增强和 gamma 校正。运行这个 GUI 界面后,你可以通过调整滑块来改变图像的增强和 gamma 校正效果,并在 axes 中查看结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值