Help with understanding how the handles structure is passed and updated in GUIs
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I looked at some tutorial videos, other questions, and this link here:
The section titled, "How can I share data between callback functions in my GUI(s)?" is quite nice, but I am confused about this part:
Since variables passed in to MATLAB functions are "pass by value" and not "pass by reference" if you change any of the variables, you are only changing the local copy. If you change any of the variables attached as members of the handle structure, and you want to retain these changed values in the calling function, you will have to return the handles structure as an output of your function, and accept it in your calling function, or else use the guidata() function. Otherwise changes you make to the handles structure variables are only local and will not survive once the function has returned.
I don't quite get it. How do I update the handles structure in a callback so that all subsequent callbacks see it? If I write in one of my callbacks
handles.MyVar = MyVar;
then will every callback that comes later be able to "see" handles.MyVar?
In my concrete example, I have a button that loads a .mat file with variables of interest. I then want to work with one of those variables in a later callback, which is from another push-button.
0 commentaires
Réponse acceptée
per isakson
le 14 Nov 2014
Modifié(e) : per isakson
le 14 Nov 2014
I assume that you use GUIDE. A callback function looks something like this (see below). handles is passed as an input argument. handles may be modified in the function. However, the modified version is lost when leaving the function - if not saved. MATLAB provides a special function guidata to save it, thus at the end of the function guidata shall be called. Next callback function that is called will get the modified handles.
function good_name_Callback( hObject, eventdata, handles )
% hObject handle to volume (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.MyVar = MyVar;
% Save the modified variable, handles
guidata( hObject, handles ) %%<<<<<<<<<<<<<<<<<<<<<<<<<<<<
end
 
In respones to comment
There is nothing special with callback functions. The "magic" is in the value of the callback property (e.g. function handle), which invokes the callback function
@( hObject, eventdata ) ...
MyGui( 'Untitled_2_Callback', hObject, eventdata, guidata(hObject) )
When invoked Matlab supplies values to hObject (and eventdata).
 
"return the handles structure as an output of your function, and accept it in your calling function"   It's difficult to write documentation. There should have been a new paragraph. This does not apply to callback functions when invoked by callbacks of graphic handle objects. Of cource it is possible to call a callback function as any other function and of course local variable values are lost at return (with exception of property values of instancies of handle classes) if not explicitely passed as output arguments. (This text will hopefully be checked and commented on if unclear or wrong.)
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Graphics Object Programming dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!