Why is the "ButtonDownFcn" callback for my axes not activated when I click on an object in the axes?

78 views (last 30 days)
I have an axes that contains several child objects. I have created a "ButtonDownFcn" callback for the axes. However, when I click on the child objects, the axes' "ButtonDownFcn" callback is not executed.
For example, I create an axis with a line using:
h = line;
set(gca,'ButtonDownFcn','disp(''axis callback'')')
when I click on the axis, I see
axis callback
displayed in the command window. However, if I click on the line, nothing occurs.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
An axes' "ButtonDownFcn" callback is only executed when you click on the axis itself, not the objects in the axis. The "ButtonDownFcn" callback for the child objects will be executed instead.
If you want the axes' "ButtonDownFcn" callback function to be executed regardless of where you click in the axes, try the following:
1. Set the "HitTest" property for the axis' child objects to 'off'. The "HitTest" property determines whether the object can become the current object when it is clicked on. If the "HitTest" property is set to 'off', then the object below it will become the current object, which could be another of the axes' child objects or the axes itself. If all of the axes' child objects have their "HitTest" property set to 'off', then the axes will become the current object and its "ButtonDownFcn" callback will be executed.
set(h,'HitTest','off')
2. Specify the desired callback function as the figure's "WindowButtonDownFcn" property. The "WindowButtonDownFcn" callback is executed whenever you click in the figure, and is executed before the "ButtonDownFcn" callback of any objects, including the figure itself.
set(gcf,'WindowButtonDownFcn','disp(''axis callback'')')
3. Define the "ButtonDownFcn" callbacks for the axes' child objects, so they have the same effect as calling the axes' "ButtonDownFcn" callback. This may include determining the object's Parent (the axes), and passing its handle as an input instead of the object's handle.
set(h,'ButtonDownFcn','disp(''axis callback'')')

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!