Restore standard interactive axes behavior (callback) when using custom ButtonDownFcn
6 views (last 30 days)
Show older comments
In the latest MATLAB versions, by default, axes got a very neet degree of interactivity.
For example, just doing
[X,Y,Z] = peaks(25);
h = surf(X,Y,Z,Z);
clicking on the axes allows to 3d orbit, zoom with the mouse wheel and show a data-tit when hovering over a point. Everything without using the "orbit", "zoom" or "data tips" buttons.
However, if we add an interactive callback to the figure, for example doing
h.ButtonDownFcn = @my_hit_fun;
function my_hit_fun(~,~)
disp('hit')
the interactive behavior is completely gone.
NOTE: we added a button down function to the surface, but the interactive behaviour is not triggered not even when clicking on the background.
Is there a way to restore this behavior , at least when 'my_hit_fun' is not triggered? Something like:
if I_hit_something_with_a_callback
execute_my_callback
else
normal_behavior
end
3 Comments
Chris Schierer
on 14 Nov 2023
Edited: Chris Schierer
on 14 Nov 2023
Hidden, undocumented, stateful graphics object functionality is a growing problem in Matlab.
In addition to the issue identified by the original post, which still occurs in Matlab 2022b, you can't restore the original functionality by removing your custom callback function. Once ButtonDownFcn is set, the state of the surface object and the axis is permanently changed. It cannot be "unset". You cannot restore the axis behavior even if you delete the surface object. You have to delete the axis and create a new one to restore this hidden functionality. (This might be a clue that the hidden functionality resides in the axis and not the object that was changed.)
Like the original poster, I have created a surface object using surf. When you mouse over the surface, you get a dot that highlights the nearest vertex, and a tooltip will appear with the coordinates of the point if you hover. The axis view angle is interactive as described by the original post. There is no functionality for "hover" or "drag" callback functions exposed to the user. If I set a ButtonDownFcn for the surface object, all of the hover functionality disappears for the surface, and the axis functionality disappears also. Setting ButtonDownFcn back to an empty character array (the default value) does not restore the original behavior. Deleting the surface does not restore the original behavior.
Chris Schierer
on 14 Nov 2023
I found a partial workaround which I tested in Matlab R2022b. These features were introduced in R2019a, so it's possible it will work on other versions after R2019a.
After removing the ButtonDownFcn from the surface (e.g., h.ButtonDownFcn='';), you can force the axis to reactivate these default behaviors by explicitly assigning the Interactions property of the axis. It seems as long as the surface has a ButtonDownFcn, these interactions are disabled. Such as:
h=surf(magic(10)); % Interactions are available in the axis where h is plotted.
h.buttonDownFcn=@myCallback; % Axis interactions are automatically disabled
h.buttonDownFcn=''; % Axis interactions are still disabled.
h.Parent.Interactions=[dataTipInteraction,rotateInteraction]; % datatip and rotate interactions are restored
The enableDefaultInteractivity function does NOT seem to restore the interactions, but explicitly assigning them does. I don't know if this is the intended behavior, but there is clearly some hidden statefulness in the interaction behavior.
Setting the Interactions property while the ButtonDownFcn is defined does not seem to change the axis behavior.
Answers (0)
See Also
Categories
Find more on Graphics Object Programming 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!