MATLAB Answers

Can someone give an easy explanation for what hObject is and what handles is in MATLAB GUI?

조회 수: 11(최근 30일)
Harryboy 10 Dec 2019
댓글: Rik 10 Dec 2019
I have not really understood what this hObjects and handles mean in context to MATLAB GUI. Therefore, due to lack of understanding I repeat my mistakes. Can someone explain the two things in an easy manner?

  댓글 수: 2

Adam 10 Dec 2019
One important distinction is that hObject, referring to a GUI component, is a 'pass by reference' object. It will always be up to date with any changes you make to it and you can retrieve the handles struct from it if you require to.
handles, on the other hand, despite its name, which may be confusing if you do OOP in Matlab where handle is the base class for 'pass by reference' type objects, is a struct, nothing more, nothing less.
You can add fields to it and retrieve fields from it, but it is 'pass by value' and will go out of scope when your function ends.
However, it is part of a mechanism via guidata, as discussed by Rik below, by which you can write it back to the GUI and get the latest from the GUI whenever you wish. But it is up to you to make sure you do that - if you don't then any changes you make in a callback function are lost after that callback.
handles = guidata( hObject )
can be used to get the handles if you need them (though in default generated callbacks handles will be passed in for you so you don't need to do this - the latest version of handles is passed into auto-generated callbacks).
guidata( hObject, handles )
will write the current version of handles back into the GUI, replacing its own version.
Be very careful with editing handles and calling this function - in particular, never write anything other than handles via guidata. It is the struct which contains all the handles to your GUI components, as well as anything you may add to it yourself, so overwriting this is not good!
gives more information on using handles and guidata.
Rik 10 Dec 2019
@Adam, I think this comment is better suited to the answer section, where I would be able to give you an upvote.

로그인 to comment.

채택된 답변

Rik 10 Dec 2019
In GUIs that are generate by GUIDE, every callback has 3 arguments:
  1. hObject: the handle to the object that generated the callback (e.g. a button). This can be retrieved with the gcbo as well.
  2. eventdata: this stores special data for some specific callbacks like key presses or scroll actions
  3. handles: the current contents of the data stored with the figure. This retrieved with guidata(hObject) at the time of the callback.
The guidata struct is very important, because GUIDE stores all handles to your elements there and it is the easiest way to share data between callbacks. Don't forget to store the modified struct back to figure (if you made any modification to the data). You can use guidata(hObject,handles) to do that.
A more thourough discussion about GUI design can be found here.

  댓글 수: 0

로그인 to comment.

추가 답변(0개)

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by