필터 지우기
필터 지우기

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

조회 수: 5 (최근 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
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.
Salad Box
Salad Box 2018년 5월 10일
Hi Jan,
Sorry about it. I have modified it. Thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
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
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.
  댓글 수: 1
Salad Box
Salad Box 2018년 5월 10일
I did set the image 'hittest' property to 'off' by putting these two lines in the Botton Down function.
im = image(RGB,'Parent',handles.axes1)
set(im,'hittest','off');
But it didn't work for some unknown reasons.:(

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by