How Does hObject Work in MATLAB GUI Callbacks?

  • Context: MATLAB 
  • Thread starter Thread starter lc99
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the use of 'hObject' in MATLAB GUI callbacks, particularly how it can be utilized to manage multiple callback functions and identify which GUI component triggered a callback. Participants explore the basic setup and functionality of 'hObject' in the context of changing properties of GUI elements.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants express limited knowledge of 'hObject' and its applications in MATLAB GUI, specifically in relation to callback functions.
  • It is proposed that 'hObject' allows a callback function to determine which GUI object invoked it, which is essential when multiple objects share the same callback.
  • Participants discuss whether 'hObject' serves merely as a shortcut for accessing properties of GUI components or if it has a more significant role in identifying the invoking object.
  • One participant suggests that using 'hObject' allows for changing the properties of the button that was pressed, such as its string value, within a shared function.
  • A code example is provided to illustrate how 'hObject' can be used to toggle the string of a button in a GUI.

Areas of Agreement / Disagreement

Participants generally agree on the utility of 'hObject' for identifying the invoking GUI component in callback functions, but there is some uncertainty about the extent of its functionality and whether it is merely a shortcut or serves a more critical purpose.

Contextual Notes

Some participants express confusion about the implementation details and the main concepts surrounding 'hObject', indicating a need for further clarification on its role in callback functions.

lc99
Messages
161
Reaction score
3
So, so far in my MATLAB class, we've only learned the bare minimum for matlab's gui.

I only know how to set up callback functions so that certain buttons can either change the background color, string, or font size.

By doing. .. handles.pushbutton.FontSize = 3 , or handles.pushbutton.BackgroundColor= [0,0,1];

This is literally all we learned...

I never learned how to use 'hObject'. How would i use it to collect the data or like combine it if i have multiple callback functions that work together to perform the same thing? What would be the purpose and the basic setup. ..? is there something like hObject.pushbutton.data = 5?
 
Physics news on Phys.org
lc99 said:
I never learned how to use 'hObject'. How would i use it to collect the data or like combine it if i have multiple callback functions that work together to perform the same thing? What would be the purpose and the basic setup. ..? is there something like hObject.pushbutton.data = 5?
Multiple callback functions is not the real motivation for hObject. If a single callback function is shared by more than one object, and the callback needs to know which object invoked it, then it can use hObject to determine that.
 
FactChecker said:
Multiple callback functions is not the real motivation for hObject. If a single callback function is shared by more than one object, and the callback needs to know which object invoked it, then it can use hObject to determine that.
i think i understand. basically, it is a short cut to calling the individual object or button?

instead of handles.button#.string, you can just call hObject.string to refer to the button?
 
lc99 said:
i think i understand. basically, it is a short cut to calling the individual object or button?

instead of handles.button#.string, you can just call hObject.string to refer to the button?
It's more than just a shortcut. It is the best way (only way?) to find out which object invoked the callback function. If there are several objects that may have invoked it, how else do you know which one it is? If that is important for the callback process, you need hObject.
 
FactChecker said:
It's more than just a shortcut. It is the best way (only way?) to find out which object invoked the callback function. If there are several objects that may have invoked it, how else do you know which one it is? If that is important for the callback process, you need hObject.

wait. maybe I am confused. let us say that i built a function that can be called by each button when it is pressed. the button passes its handles and object structure to the function.

now, let's say i want the string of the button that is pressed to change its string to 'x'.

i would write in the function, hObject.string = 'x' . is that the main concept of hobjects? to be able to do that?
 
lc99 said:
wait. maybe I am confused. let us say that i built a function that can be called by each button when it is pressed. the button passes its handles and object structure to the function.

now, let's say i want the string of the button that is pressed to change its string to 'x'.

i would write in the function, hObject.string = 'x' . is that the main concept of hobjects? to be able to do that?
That sounds right to me. I am not an expert in this stuff, so maybe someone with more expertise can confirm or correct.
 
Here, try this code and see if it does what you are talking about.

Code:
function [] = change_string
% Deomonstration GUI.
H.fig = figure('units','pixels','position',[100,100,200,100],...
               'menubar','non');
H.pushbutton = uicontrol('style','pushbutton','units','pixels',...
                         'position',[0,0,200,100],...
                           'string','State 1',...
                           'callback',@myfunc);
                      
function [] = myfunc(hObject,eventdata,varargin)

if strcmp(hObject.String,'State 1')
    hObject.String = 'State 2';
else
    hObject.String = 'State 1';
end
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
5K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 2 ·
Replies
2
Views
16K
  • · Replies 1 ·
Replies
1
Views
8K