MATLAB: Try to display a different image on an axes everytime I click on the axes

axesaxes clickguiimage click

Hi bros and sis,
————————————————————-
Say I have three images named '1.jpg', '2.jpg', and '3.jpg' showing below.
————————————————————-
I also created an axes on a Gui. Showing below.(I have removed the outlines of the axes.)
————————————————————-
What I would like to achieve is that everytime when I click on the axes, it will display a different image, which means 1st time when I click axes it will display '1.jpg'; when I click on the axes again, it will display '2.jpg'; if I click on the axes again, it will display '3.jpg'.
————————————————————-
My problem is that the first time when i click on the axes, it displayed '1.jpg' without problem, but when i click on the axes again, it has no respond to my second click ( see figure below) and never displayed '2.jpg', not to say '3.jpg'.
————————————————————-
In the opening function I defined a variable 'handles.Index' and a 'ButtownDownFcn' named 'click'. See below.
% --- Executes just before t1 is made visible.
function t1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to t1 (see VARARGIN)
% Choose default command line output for t1
handles.output = hObject;
set(handles.axes1,'xtick',[]);
set(handles.axes1,'ytick',[]);
set(handles.axes1,'xcolor',[0.94 0.94 0.94]);
set(handles.axes1,'ycolor',[0.94 0.94 0.94]);
handles.Index = 0;
set(handles.axes1,'ButtonDownFcn',@click);
% Update handles structure

guidata(hObject, handles);
————————————————————-
In the 'ButtonDownFcn' named 'click', I wrote:
function click(hObject,eventdata)
handles = guidata(hObject);
handles.Index = handles.Index + 1;
fn = sprintf('%d.jpg',handles.Index);
RGB = imread(fn);
im = image(RGB,'Parent',handles.axes1)
set(im,'hittest','off');
% Update handles structure
guidata(hObject,handles);
————————————————————-
Anything mistakes I made in the 'ButtonDownFcn' part that stops respond to a mouse click?? Can anyone help please?
Many thanks!

Best Answer

This happens, because the mouse click event is caught be the object in the foreground, the image. It steals the event and does not let it down to the axes in the background.
You tried to make the image transparent for events by setting hittest to 'off'. This is the right way, actually. But the 'NextPlot' property of the axes has the default value 'replace'. Then high-level functions like image reset the axes' properties to the defaults, such that the ButtonDownFcn is cleared.
So either set 'NextPlot' to 'add' (equivalent to: hold on). Or set the ButtonDownFcn again after image. Alternatively you can use the ButtonDownFcn of the image:
  • Method 1:
% in t1_OpeningFcn()
...
set(handles.axes1, 'NextPlot', 'add');
...
% in click():
% Now you have to remove former images manually:
delete(get(handles.axes1, 'Children'));
im = image(RGB, 'Parent', handles.axes1, 'HitTest', 'off');
  • Method 2:
% in click()

im = image(RGB, 'Parent', handles.axes1, 'HitTest', 'off');
set(handles.axes1, 'ButtonDownFcn', @click);
  • Method 3:
% in click()
im = image(RGB, 'Parent', handles.axes1, 'ButtonDownFcn', @click);
Related Question