How Does hObject Work in MATLAB GUI Callbacks?

  • Context: MATLAB 
  • Thread starter Thread starter lc99
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 2K views
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?
 
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