Help with understanding how the handles structure is passed and updated in GUIs

3 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

per isakson
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
&nbsp
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).
&nbsp
"return the handles structure as an output of your function, and accept it in your calling function" &nbsp 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
Alexei
Alexei le 14 Nov 2014
Thank you! That's exactly what I was suspecting. Then what is the other scenario that they propose? I'm talking about this part:
If [...] 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...
What would be the code for this? Would I have to change the top line of my callback to include an output argument? Instead of
function callback_name( hObject, eventdata, handles)
to
function(handles) callback_name( hObject, eventdata, handles )
?
per isakson
per isakson le 14 Nov 2014
See addendum to the answer above.

Connectez-vous pour commenter.

Plus de réponses (0)

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!

Translated by