MATLAB Answers

0

How do I trigger ButtonDownFcn callback for an object with visible off in MATLAB R2014b?

How do I trigger ButtonDownFcn callback for an object with visible off in MATLAB R2014b?

Products


Release

R2014b

1 Answer

Answer by MathWorks Support Team on 7 Oct 2014
 Accepted Answer

Starting in MATLAB R2014b, graphics objects have a new PickableParts property that controls if the object can capture mouse clicks. By default, the PickableParts property is set to 'visible' so the object can capture mouse clicks when the Visible property is set to 'on'. If you want an invisible object to capture mouse clicks, then set the PickableParts property to 'all'.
For example, create a marker and set the ButtonDownFcn callback so that clicking the marker displays 'hit' at the command line.
m = line(5,5,'Marker','o','MarkerSize',20,'ButtonDownFcn','disp hit');
Set the Visible property of the marker to 'off' to make it invisible.
m.Visible = 'off';
axis([4 6 4 6])
Clicking over the position of the invisible marker does not trigger the ButtonDownFcn callback because invisible objects do not capture clicks by default. Enable the invisible marker to capture clicks by setting the PickableParts property to 'all'.
m.PickableParts = 'all';
Note: When an object captures a mouse click, the HitTest property controls if the object responds to the click or if it passes the click to an ancestor. By default, the object responds. Set the HitTest to 'off' if you want an ancestor to respond, such as the parent axes.

  4 Comments

Show 1 older comment
For releases before R2014b, you could always define the callback for the underlying object as a workaround. For axes, this will be the figure most of the time (probably a uipanel sometimes). Example:
surf(peaks) set(gca,'Visible','off'); set(gcf,'ButtonDownFcn','disp hit')
For releases before R2014b, you could always define the callback for the underlying object as a workaround. For axes, this will be the figure most of the time (probably a uipanel sometimes). Example:
surf(peaks) set(gca,'Visible','off'); set(gcf,'ButtonDownFcn','disp hit')
For releases prior to R2014, you could define the callback of the underlying object (which will be a figure or uipanel in most cases). Example: surf(peaks) set(gca,'Visible','off'); set(gcf,'ButtonDownFcn','disp hit')

Sign in to comment.