MATLAB Answers


Handle and hobject in Matlab GUI

Usman Rashid さんによって質問されました 2019 年 1 月 30 日
最新アクティビティ Rik
さんによって 編集されました 2019 年 1 月 30 日
I want to know that what is actually difference between handles and hobject in Matlab GUI?
In which cases we can use handles and in which cases we have to use hobjects.
It is somehow confusing.

  1 件のコメント

2019 年 1 月 30 日
The command line is always your friend when you want to learn about what different objects are. Put a breakpoint in and simply type:
on the command line and then
and you will see exactly what each of them are. Being a struct handles can contain literally anything, in addition to the handles of the GUI components in your figure, which is where it gets its common name from.
When you first use GUIDE handles seems like some magic variable that is always there and has mysterious properties that allow it to be visible in all your callbacks, but it is simply a struct. It is just made available as an input argument through the callbacks that are automatically created through GUIDE, but within the scope of a function is is like any other struct, it just happens to contain handles to all your GUI objects.

サインイン to comment.





1 件の回答

回答者: Rik
2019 年 1 月 30 日
編集済み: Rik
2019 年 1 月 30 日

You should not be using GUIDE in the first place.
The first input to a callback function is a handle to the callback object, which is often given the name hObject. On the other hand handles is the name that is often given to the struct that is saved with guidata. You can load or save that struct with the following lines.
%hObject is either a handle to the GUI figure, or an object that is a child of the GUI
My small guide to avoid GUIDE:
  • Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
  • Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
  • When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
  • You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
  • Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
  • More information about callbacks can be found in multiple places in the doc, for example here.

  0 件のコメント

サインイン to comment.

Translated by