Ó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;
handles = guidata(hObject);
...
coordinates=get (gca, 'CurrentPoint');
handles.coordinates=coordinates;
...
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)
...
set(handlesStacked(:),'ButtonDownFcn', {@axes2_ButtonDownFcn, hObject});
Now in your callback you change its signature and do
function axes2_ButtonDownFcn(hObject, eventData, hFigure)
global position;
handles = guidata(hFigure);
...
coordinates=get (gca, 'CurrentPoint');
handles.coordinates=coordinates;
...
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.
Best Answer