How to register mouse clicks when a image object is created

Hi, I've been trying to create a GUI which displays an image and asks the user the identify a point on that image. However once the image is created my button down callbacks stop triggering.
The code I'm using prior to asking the user to identify a point is:
HC=get(handles.acp_wind,'Children');
[m,~]=size(HC);
for i=1:m
set(HC(i),'HitTest','off')
end
after that I use the ButtonDownFcn of acp_wind, which is an axes object, to trigger on each mouse click and get the current point. Unfortunately the ButtonDownFcn doesn't trigger on mouse clicks once the image is created but works fine without an image. i've also tried triggering the function whenever the user clicks inside the GUI window by using:
set(gcf,'WindowButtonDownFcn',@acp_wind_ButtonDownFcn)
But again this callback stops registering when the image object is created.
Any Ideas what I'm doing wrong?
Edit: I've also tried setting the ButtonDownFcn of the image object to trigger the acp_wind_ButtonDownFcn fucntion using:
BD=get(handles.acp_wind,'ButtonDownFcn');
set(HC,'ButtonDownFcn',BD)

3 Comments

Side note: you can rewrite your code as
set( get(handles.acp_wind,'Children'), 'HitTest', 'off' );
What didn't work with:
BD=get(handles.acp_wind,'ButtonDownFcn');
set(HC,'ButtonDownFcn',BD)
This looks like the approach I would take. Then instead of creating a new image object, just change the 'cdata' of the current image.

Sign in to comment.

 Accepted Answer

Why not simply use ginput(1)? That's what I'd do. However if you want to do it by your way (which I don't recommend), let me show you how I set up a click event for the image itself. Here I have it so that if you click on the image, it reads in another random splash image. Read this code and especially the comments for further explanation:
This code is inside the function LoadSplashImage. Pay particular attention to how you have to set up the function handles as a cell array and with an @ symbol if you want to pass in arguments to it (such as the handles structure):
% Read in splash image and display it in the axesImage axes.
if exist(strSplashImageFileName, 'file')
% Read in the image from the disk file.
imgSplash = imread(strSplashImageFileName);
% Display image array in the specified axes on the user interface,
% storing handle of image for later quirk workaround.
set(handleToImageAxes, 'visible', 'on');
axes(handleToImageAxes);
cla reset; % Clear any additional images that may be hiding in the axes control (you can have multiple images).
% Display the image, and save the handle of the image object.
axesChildHandle = imshow(imgSplash);
axis('off'); % Turn off tick marks, in case they were showing.
% !!!! QUIRK workaround.!!!!
% Make it so that if they click in the image axes, it will execute the
% button down callback called "MyCustomAxesButtonDownFunction".
% Now it won't, because the image is "covering up" the axes so the
% axes never sees the click, only the image does. But if you haven't
% set up a click event for the image, there is nothing for it to do.
% Set up a ButtonDownFcn click event for the actual image itself (separate from the axes).
set(axesChildHandle, 'ButtonDownFcn', {@MyCustomAxesButtonDownFunction, handles});
% !!!! End QUIRK workaround.!!!!
else
errorMessage = sprintf('\nThe splash image, %s, was not found.\n', strSplashImageFileName);
fprintf('%s\n', errorMessage);
end
Code for the function "MyCustomAxesButtonDownFunction". This is the code that gets run when you click on the image. Note the use of the required two dummy arguments:
% Custom callback that gets called when the user clicks on the image in the handles.axesImage axes.
function MyCustomAxesButtonDownFunction(dummy1, dummy2, handles)
try
% Load another, random splash image into the axes.
LoadSplashImage(handles, handles.axesImage);
catch ME
errorMessage = sprintf('Error in function MyCustomAxesButtonDownFunction.\n\nError Message:\n%s', ME.message);
fprintf('%s\n', errorMessage);
WarnUser(errorMessage);
end
return; % from MyCustomAxesButtonDownFunction

More Answers (3)

I'm considering reverting to that but the goal was to make the selection step non-modal and ginput is modal.

Categories

Find more on Images in Help Center and File Exchange

Asked:

on 30 Jul 2012

Answered:

on 26 Sep 2014

Community Treasure Hunt

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

Start Hunting!