ButtonDownFcn on Axes after imshow

10 views (last 30 days)
My ButtonDownFcn for my axes does not work after I call imshow
ax = axes;
set('ButtonDownFcn', @(h,e) disp('Hello'));
imshow(img, 'Parent', ax);
Of course I've read the myriad other post with the same problem and the solution seems to be something like this:
ax = axes;
set('ButtonDownFcn', @(h,e) disp('Hello'));
hold on
h = imshow(img, 'Parent', ax);
set(h, 'HitTest', 'off');
When I do this, it does indeed turn off the hittest for the image, but unfortunately when I click the image I am hitting the figure, not the axes. This is confirmed by the following:
>> hittest()
ans =
Figure (1) with properties:
Number: 1
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [1232 1208 570 450]
Units: 'pixels'hittest()
So how do I force my click to hit the axes and not the figure? My only work around is to set the ButtonDownFcn for the image itself, but this is really not desirable because after some user input I need to overlay (with transparency) a new image which then becomes the top image. I could then set an identical ButtonDownFcn on this new image, but I'm hoping for a better solution than this work around.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Apr 2017
Make sure that hittest and pickableparts is set for the axes.
imshow() does a lot of things behind your back. I prefer to use image()
  7 Comments
Alex Nieva
Alex Nieva on 12 Jul 2018
Edited: Alex Nieva on 12 Jul 2018
For me I had everything in place except the hold on after setting up the axes. Then it worked.
Walter Roberson
Walter Roberson on 12 Jul 2018
The difficulty with the very first version of the code the user posted is that it sets up something on the axes and then calls imshow. When imshow detects that it is being called on an axes that is in the default position for a single axes then imshow does not merely change the content of the axes but instead deletes the axes. Which of course gets rid of the callback that has been set on the axes. imshow does not do the deletion if hold on is in effect.
Also, of course, imshow is adding the image object, which covers up the axes, so for the axes button callback to work on the image you have to configure for hits to get passed through the image object.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 26 Apr 2017
Create a callback function and specify that callback function in the callback of the figure, not the axes.
  1 Comment
Michael Vaiana
Michael Vaiana on 27 Apr 2017
Yes, this is another work around, but it turns out that setting pickable parts to 'all' achieved exactly what I was looking for :). Thanks for the suggestion!

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!