I am fairly new to writing GUIs in Matlab and noticed that when passing information between callbacks usually two values hObject
and handles
are passed.
From what I read and understood, hObject
is the handle to an object which contains the real data (or at least handles to it) and handles
is not itself a handle, but a struct reproducing the structure of objects "behind" hObject
. Changing (or adding to) handles
does not change the real data as it is seen from the calling function but a local copy. To actually write the changed data into the object pointed to by hObject
I need to call guidata(hObject, handles)
.
Is this right so far or did I get something wrong?
I also read that I can create struct similar to handles
by calling handles = guidata(hObject)
.
So is there any point in passing both hObject
and handles
to one of my own functions instead of just passing hObject
and creating handles
locally?
You are correct so far. The input parameter handles
is a handy way of keeping track of all the components of the UI. As a standard handles
is not an input parameter. But if you use guide
it will set is as an extra parameter as follows by setting for example the Callback
option to the anonymous function @(hObject,eventdata)guitest('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
.
This can of course just as easily be done in the code for the function by handles = guidata(hObject)
.
Usually you will not need to change in the handles
(only in the objects they refer to), but if you need to change something in the handles
(for example if you use it to store more than just the handles
for the UI elements) it is important to call guidata(hObject, handles)
to actually save the changes.
So the short answer to you question is: no, you can just as well retrieve the handles
yourself when you need them.