Why Is Not the Handles Variable Updated?
Afficher commentaires plus anciens
I have made a simple GUI in GUIDE. I update field A by function myfun1 to the value 20, but I still get 1 in the Command Window! Why hasn’t handles variable been updated? What is the solution? I’d appreciate it if you explain it elaborately
function pushbutton1_Callback(hObject, eventdata, handles)
handles.A = 1;
guidata(hObject, handles);
myfun1(hObject, eventdata, handles)
disp(handles.A)
function myfun1(hObject, eventdata, handles)
handles.A = 20;
guidata(hObject, handles);
2 commentaires
In many senses 'handles' is a very unfortunate name for this structure. Less so if you have never done any OOP and are not familiar with handle-derived classes, but still can be confusing when people refer to handles to graphic objects also.
Walter's answer describes this largely, but just to add a little: 'handles' in a GUIDE GUI is literally just a struct, nothing more. It gets passed around to callbacks in a non-transparent way that makes it look like some kind of more magic object, but it is just a struct and works as any other struct.
Graphics objects (including any components in your GUI such as pushbuttons) use handle class semantics - i.e. they copy by reference like any class that derives from the 'handle' base class, but this should not be confused with the 'handles' struct.
So if you are updating a property of e.g. a pushbutton in a function then you do not need to return handles and reassign it - the change will happen automatically, e.g.
handles.pushbutton1.String = 'Some String';
does not require you to do anything more, but
handles.A = 20;
does, as discussed in the answers below.
Jan
le 17 Mar 2017
+1. I've voted for this thread. The question is clear and it is a common problem. The different answers and comments focus on different viewpoints and complement each other. I made a typo in my marginal note, and Walter has clarified it. This is the kind of cooperative teamwork I like in this forum. If a reader has a problem with the updates of the "handles" struct, reading this thread will help.
Thanks, Adam, Chocolate, Rightia and Walter.
Sorry, this comment might look off-topic. I think that positive feedback is more satisfying than complains in case or problems.
Réponse acceptée
Plus de réponses (2)
ES
le 17 Mar 2017
Your code should work.
Alternatively you can make myfun1 to return handles instead of using guidata to update handles.
function pushbutton1_Callback(hObject, eventdata, handles)
handles.A = 1;
guidata(hObject, handles);
handles = myfun1(hObject, eventdata, handles)
disp(handles.A)
function handles = myfun1(hObject, eventdata, handles)
handles.A = 20;
4 commentaires
Walter Roberson
le 17 Mar 2017
No the code will not work
+1. I assume this code does work: The handles struct is updated inside myfun1 and returned to the called. Then disp(handles.A) replies 20. But when Matlab returns from pushbutton1_Callback, the changes of handles are forgotten. It is not clear if this is wanted of if a trailing guidata(hObject, handles) is required at the bottom of the callback. E.g.:
function pushbutton1_Callback(hObject, eventdata, handles)
handles.A = 1; % --> update by GUIDATA not needed here
handles = myfun1(hObject, handles); % [EDITED, see below]
disp(handles.A)
guidata(hObject, handles); % <-- final update
function handles = myfun1(hObject, handles)
handles.A = 20;
@Walter: Perhaps I've overseen a detail?
Alternatively:
function pushbutton1_Callback(hObject, eventdata, handles)
handles.A = 1;
guidata(hObject, handles); % Store changed struct inGUI
myfun1(hObject, eventdata, handles);
handles = guidata(hObject); % <-- obtain changed struct from GUI
disp(handles.A)
function myfun1(hObject, eventdata, handles)
handles.A = 20;
guidata(hObject, handles); % Store changed struct in GUI
[EDITED] A mistake of mine: I forgot the "handles=", but in Chocolate Warrior's code this was included correctly.
Walter Roberson
le 17 Mar 2017
Jan, your code returns handles from myfun1 but does not assign it back to handles, so at the time of the disp() you are going to be using the unmodified version as if the call had not been made.
Chocolate Warrior's code does assign back but misses the guidata.
But my line about the code not working was intended to refer to where Chocolate Warrior said that Rightia's code should work.
Jan
le 17 Mar 2017
@Walter: Thanks for the clarification and finding the bug in my code. See [EDITED]: I had copied Rightia's code accidently, but Chocolate's code contained the required "handles=". Now I understand what "does not work" was related to.
Jan
le 17 Mar 2017
0 votes
Sharing data between callbacks is a frequently occurring problem. Therefore searching in the forum is most likely useful:
Catégories
En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!