Effacer les filtres
Effacer les filtres

ButtonDownFcn does not work on a plot

3 vues (au cours des 30 derniers jours)
Filippo Mayer
Filippo Mayer le 18 Août 2023
Commenté : Dave B le 18 Août 2023
I want to add a Button Down Callback Function to my code so that if I click on a line it does something (e.g. it displays a phrase or modifies some properties etc.).
The code is
f1 = figure;
f1.Name = 'Figure 1';
f1.MenuBar = 'none';
% plot p2
position = [-0.5774; 0; -0.8165];
p2 = plot3(position(1),position(2),position(3),'k.','MarkerSize',15);
hold on
% plot p3
position = [0; 0; 0; position];
p3 = plot3([position(1) position(4)], ...
[position(2) position(5)], ...
[position(3) position(6)],'k');
bound = 2;
box = [-bound bound -bound bound -bound bound];
% UI
p2.ButtonDownFcn = @touch_callback;
function touch_callback(~,~)
If I run the code and click on the object to which the callback function is related nothing happens, but if I comment lines 10-12 it works correctly.
What I want to know is why this happens and if I can make the Callback work with the lines 10-12 not commented.

Réponse acceptée

Dave B
Dave B le 18 Août 2023
How about if you set p3.PickableParts = 'none'? If you're clicking the part of p2 that is on top of p3, then MATLAB is probably associating that click with p3.
Alternatively, if you plan to have a mouse interaction with p3, you might swap the order (e.g. with @doc:uistack, or ax=gca;ax.Children = flip(ax.Children);, or just plotting them in the opposite order).
  1 commentaire
Dave B
Dave B le 18 Août 2023
Oh and if you want to include both the line and marker in your ButtonDown, you might consider making them with one object for simplicity:
position = [0 -0.5774; 0 0;0 -0.8165];
p3 = plot3([position(1) position(4)], ...
[position(2) position(5)], ...
[position(3) position(6)],'k.-',...
axis([-2 2 -2 2 -2 2])

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 18 Août 2023
You set the ButtonDownFcn against p2, which is a single point.
Your line is p3, but you do not have a callback configured against it.
Your p3 starts directly on top of p2, and it is drawn after p2 is drawn. p3 is "eating" the request.
If you want an object "underneath" to be able to react to callbacks, then you need to disable callback processing on the top object. See HitTest and PickableParts https://www.mathworks.com/help/matlab/creating_plots/capturing-mouse-clicks.html


En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by