How can I save the handles of a GUI in other .m file?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Geoff Hayes
2014-11-29
编辑:Geoff Hayes
2014-11-29
Ó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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!