Try to display a different image on an axes everytime I click on the axes
2 次查看(过去 30 天)
显示 更早的评论
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!
2 个评论
Jan
2018-5-10
Please post code as text, not as screenshots. Then it is easy to suggest modifications by copy&paste, while retyping the code requires a lo of scrolling.
采纳的回答
Jan
2018-5-10
编辑:Jan
2018-5-10
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);
更多回答(1 个)
Image Analyst
2018-5-10
If I recall correctly, it involves setting the hittest property of the axes control or the image itself to off since the figure is not seeing your click unless you set the hittest of the above layers to off.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!