How can I save the handles of a GUI in other .m file?
1 view (last 30 days)
Show older comments
I have a GUI, and when the user click in one axes I call this function:
set(handlesStacked(:),'ButtonDownFcn', {@axes2_ButtonDownFcn, handles});
As you can see I send the handles, and this function is defined in other .m file:
function axes2_ButtonDownFcn(hObject, eventData, handles)
global position;
...
coordinates=get (gca, 'CurrentPoint');
handles.coordinates=coordinates;
...
guidata(hObject, handles);
end
Everything work but last line: 'guidata(hObject, handles);' which doesn't save the handles... Who could I do to save them inside the file?
Thanks
0 Comments
Accepted Answer
Geoff Hayes
on 29 Nov 2014
Edited: Geoff Hayes
on 29 Nov 2014
Óscar - you have to be careful with the line of code
set(handlesStacked(:),'ButtonDownFcn', {@axes2_ButtonDownFcn, handles});
The handles structure that is being passed as the third input parameter to axes2_ButtonDownFcn is a copy of the handles structure at the time that this set is called. So whenever the axes2 callback fires, the old copy of handles will be passed in...not the most recent version which is what you want.
You can try to do the following instead
set(handlesStacked(:),'ButtonDownFcn', @axes2_ButtonDownFcn);
with the callback defined as
function axes2_ButtonDownFcn(hObject, eventData)
global position;
% get the latest copy of *handles*
handles = guidata(hObject);
...
coordinates=get (gca, 'CurrentPoint');
handles.coordinates=coordinates;
...
% save this version of the handles structure
guidata(hObject, handles);
end
In the above code, we assume that hObject is a child of the parent figure (your GUI). See guidata for details.
-------
If the above assumption is not true, and so hObject is not a child of the parent figure (GUI), then using guidata to get and set the handles structure will fail. In that case, you can make a slight change to your original code and pass the parent figure handle as the third input to your callback.
Assuming that your set is called from the OpeningFcn of your GUI, then we can do
function myGui_OpeningFcn(hObject, eventdata, handles, varargin)
% do stuff
...
set(handlesStacked(:),'ButtonDownFcn', {@axes2_ButtonDownFcn, hObject});
% etc.
Now in your callback you change its signature and do
function axes2_ButtonDownFcn(hObject, eventData, hFigure)
global position;
% get the latest copy of *handles*
handles = guidata(hFigure);
...
coordinates=get (gca, 'CurrentPoint');
handles.coordinates=coordinates;
...
% save this version of the handles structure
guidata(hFigure, handles);
end
So when getting and setting the handles structure, we use the parent figure handle, hFigure.
You may want to try the second method as it is more similar to your original implementation.
0 Comments
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!