필터 지우기
필터 지우기

How can I save the handles of a GUI in other .m file?

조회 수: 3 (최근 30일)
Óscar
Óscar 2014년 11월 11일
편집: Geoff Hayes 2014년 11월 29일
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

채택된 답변

Geoff Hayes
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개)

카테고리

Help CenterFile Exchange에서 Graphics Object Identification에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by