ButtonDownFcn on Axes after imshow

15 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2017-4-26
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 个评论
Alex Nieva
Alex Nieva 2018-7-12
编辑:Alex Nieva 2018-7-12
For me I had everything in place except the hold on after setting up the axes. Then it worked.
Walter Roberson
Walter Roberson 2018-7-12
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.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2017-4-26
Create a callback function and specify that callback function in the callback of the figure, not the axes.
  1 个评论
Michael Vaiana
Michael Vaiana 2017-4-27
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!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Display Image 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by