MATLAB How Does hObject Work in MATLAB GUI Callbacks?

  • Thread starter Thread starter lc99
  • Start date Start date
AI Thread Summary
The discussion centers on the use of MATLAB's GUI and the role of 'hObject' in callback functions. Participants express a basic understanding of setting up GUI elements like buttons and modifying their properties, such as background color and font size. The main point of confusion involves how to effectively use 'hObject' to identify which button triggered a callback, especially when multiple buttons share the same callback function. It is clarified that 'hObject' is essential for determining the specific object that invoked the callback, allowing for more dynamic and flexible programming. An example is provided to illustrate how 'hObject' can be used to change the string of a button when pressed, confirming that this is a key concept in managing interactions within the GUI. Overall, the discussion emphasizes the importance of understanding 'hObject' for effective GUI programming in MATLAB.
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
Views
3K
Replies
6
Views
4K
Replies
3
Views
2K
Replies
2
Views
2K
Replies
12
Views
3K
Replies
2
Views
16K
Replies
4
Views
3K
Back
Top