Purpose of guidata(hObject,handles)

34 visualizaciones (últimos 30 días)
Amanda
Amanda el 1 de Sept. de 2012
Comentada: Mike D. el 19 de Sept. de 2016
I've been working with GUI GUIDE.
What is the purpose or role of guidata(hObject,handles)?
Thanks,
Amanda

Respuesta aceptada

Sven
Sven el 1 de Sept. de 2012
Editada: Sven el 1 de Sept. de 2012
Every graphics "object" (a figure, an axes, a button, a textbox etc) is represented by a "handle" variable. That's the hObject.
If you're making a GUI, you actually want to store data about the "state" of the program inside the GUI somewhere. Think of a GUI with 2 buttons (one with a "+", one with a "-") and a text display that starts at "1". If the user clicks the +, that display should increment. If the user clicks the "-" that display should decrement.
Somewhere in the GUI, a "counter" variable needs to be stored. One way of doing it is storing it in the "guidata" of your figure's GUI. That's a special location specifically for GUIs. I think that there are two reasons why it's special:
  1. Every hObject (a pushbutton, an axes, etc) that is a child of the same figure points to the same "guidata". So it's a common place for all parts of a GUI to share data.
  2. Any callback function in the GUI automatically gets the guidata as its third argument. That is, whenever you click a button of a GUI with some guidata set, the input parameters (by default) is always a copy of guidata(GUIhandle), made immediately at the time of clicking the button.
So a callback function will look like:
function pushButtonCallback(buttonHandle, eventData, handles)
...
disp(handles)
Now, handles is just a structure, so let's say that we've stored the counter that we're using in handles.counter. In other words, somewhere at the start of your GUI you had lines like:
handles = guidata(hObject);
handles.counter = 1;
guidata(hObject, handles);
When they hit the "+" button, we want to increment handles.counter, but we also want this new value available to every other callback. To do that, we need to update the handles.counter field, and then put handles back into the GUIs guidata. As soon as we've done that, the next time the user clicks a button, they'll be using the newly updated version of handles.
function plusPushButtonCallback(buttonHandle, eventData, handles)
% At THIS point, "handles" is just a copy of guidata for the GUI
handles.counter = handles.counter + 1;
% At THIS point, "handles" is different to the guidata for the GUI
guidata(buttonHandle, handles);
% At THIS point, "handles" has been put back in as new guidata for the GUI
Did that make things a little clearer Amanda?
  6 comentarios
harjas kaur
harjas kaur el 16 de Sept. de 2016
WOW, your answer was more clear than any video demonstration. Also it has made things of the GUI more interesting. Thanks a lot
Mike D.
Mike D. el 19 de Sept. de 2016
You say "Any callback function in the GUI automatically gets the guidata as its third argument", but this is not true for a programmatic GUI. A programmatic GUI can get the stored data by typing handles = guihandles(hObject).

Iniciar sesión para comentar.

Más respuestas (2)

Matt Fig
Matt Fig el 1 de Sept. de 2012
Editada: Matt Fig el 1 de Sept. de 2012
This stores the variable named 'handles' in the guidata of the object with handle HObject.
I assume you saw this in the Openfcn of a GUIDE GUI, correct? If so, then this line is simply storing a structure called 'handles', which contains the handles to all visible graphics objects, in the guidata of the figure. From anywhere else you can access this structure by calling GUIDATA(hfig). For example, say you have a GUIDE GUI named mygui. Do this at the command line:
H = mygui; % Returns the handle of the figure into H.
guidata(H)
  1 comentario
Amanda
Amanda el 1 de Sept. de 2012
Wow! Thanks for the answer. I understand completely.

Iniciar sesión para comentar.


arestw ali
arestw ali el 25 de Oct. de 2015
hello everyone what is the different between (hObject, eventdata, handles, varargin) how can i related between for exmp. the pushbutton and pop,text,axis thanx a lot
  1 comentario
Walter Roberson
Walter Roberson el 25 de Oct. de 2015
hObject is the handle to the object whose callback is being executed.
eventdata is empty for most kinds of callbacks, but for some kinds of callbacks it gives additional information about what triggered the callback. For example a key press function callback will have information about which key sequence triggered the callback.
handles, in the context you use, is a structure containing whatever data has been stored in it. When you are using GUIDE, at the very least it will have one field for every graphics object you created using GUIDE, with the field named the same as the Tag you used for the object. For example if you have a pushbutton1 and an editbox1 then handles would be a structure with fields "pushbutton1" and "editbox1", and the value of those fields created by default would be the handles of the respective graphics object. It is common to also store other non-handle information into handle, such as the way Sven showed storing a counter there.
varargin is for variable length argument data; see http://www.mathworks.com/help/matlab/ref/varargin.html. It allows the routine to be programmed to accept optional parameters.

Iniciar sesión para comentar.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by