How Does hObject Work in MATLAB GUI Callbacks?

  • MATLAB
  • Thread starter lc99
  • Start date
In summary, so far in my MATLAB class, we've only learned the bare minimum for matlab's gui. We learned how to set up callback functions so that certain buttons can either change the background color, string, or font size. But by doing this, we never learned how to use 'hObject'. How would we use it to collect the data or like combine it if we had multiple callback functions that worked together to perform the same thing? What would be the purpose and the basic setup. ..? is there something like hObject.pushbutton.data = 5?
  • #1
lc99
161
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
  • #2
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.
 
  • #3
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?
 
  • #4
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.
 
  • #5
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?
 
  • #6
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.
 
  • #7
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
 

1. What is a handle in MATLAB?

A handle in MATLAB is a variable that stores a reference to another object or function. It is used to access and manipulate the properties and methods of the referenced object or function.

2. How are handles created in MATLAB?

Handles can be created in MATLAB using the handle function, which takes in the referenced object or function as an input argument. Alternatively, handles can also be created using the uicontrol or uimenu functions for creating user interface objects.

3. What is the difference between a handle and hObject in MATLAB?

A handle is a general term for a variable that stores a reference to an object or function. hObject, on the other hand, is a specific handle that refers to the graphical user interface (GUI) object that is currently being manipulated. In other words, hObject is a handle that is automatically created and passed to callback functions when a GUI object is interacted with.

4. How are handles and hObject used in GUI programming?

Handles and hObject are essential in GUI programming because they allow for easy manipulation of GUI objects and their properties. They are used to access and modify the appearance, behavior, and data of GUI objects, as well as to handle user interactions with the GUI.

5. Can handles and hObject be used in non-GUI programming in MATLAB?

Yes, handles and hObject can be used in non-GUI programming in MATLAB. They are commonly used in object-oriented programming to facilitate the manipulation of objects and their properties. They can also be used to pass functions as arguments to other functions, allowing for more flexible and dynamic programming.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
12K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
Back
Top