MATLAB: Get coordinates from image (imagesc) in guide

coordinatesguideimagesc

Hi All, I may have a misunderstanding of how data habndling or axes work when using guide. Here's my problem: I create a basic GUI with two axes (axes1 and axes2), and one static text.
When clicking axes1 I want to display the coordinates in the static text:
function axes1_ButtonDownFcn(hObject, eventdata, handles)
coord = handles.axes1.CurrentPoint;
handles.text2.String = sprintf('%f ',coord);
guidata(hObject, handles);
This works, I get three pairs of values when I click in the figure.
However, when I load data and put the data into axes1, clicking does not work anymore…
function GuideTest_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.results = load('results.mat');
imagesc(handles.axes1,handles.results.Fr1);
guidata(hObject, handles);
The data which I expect is displayed nicely in the figure, but clicking does not seem to do anything anymore… What am I missing regarding handles, data transfers or callbacks?
Thanks! Karel

Best Answer

There are two parts to your problem.
The first reason you see this behaviour is because you're using the High-Level version of images. The High-Level version calls newplot which in turns reset pretty much all the axes properties if the axes NextPlot is the default 'replace'.
There are several ways you can prevent that. You can use the low-level version of imagesc instead:
imagesc(handles.axes1, 'CData', handles.results.Fr1);
or set the NextPlot property of the axes to 'replacechildren':
handles.axes1.NextPlot = 'replacechildren';
The downside of them is that a bunch of things that normally happen (flipping of the axes, adjusting xlim and ylim) doesn't get done and you'd have to do that manually.
So, probably the best thing is to keep things as is, and recreate the callback after the call to imagesc:
imagesc(handles.axes1, handles.results.Fr1);
handles.axes1.ButtonDownFcn = @(hObject, eventdata) yourguiname('axes1_ButtonDownFcn', hObject, eventdata, guidata(hObject));
However, if there are other properties of the axes that were non-default, you'd have to reassign them as well.
The second part to the problem is that by default the image object created by imagesc captures the mouse clicks and prevent the axes from seeing them. That is easily fixed by setting the HitTest property to 'off' which lets the parent of the image sees the clicks:
imagesc(handles.axes1, handles.results.Fr1, 'HitTest', 'off');
Alternatively, associate the callback with the image object instead of the axes.