ButtonDownFcn does not work on a plot

10 visualizaciones (últimos 30 días)
Filippo Mayer
Filippo Mayer el 18 de Ag. de 2023
Comentada: Dave B el 18 de Ag. de 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];
axis(box)
% UI
p2.ButtonDownFcn = @touch_callback;
function touch_callback(~,~)
disp('ciao')
end
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.

Respuesta aceptada

Dave B
Dave B el 18 de Ag. de 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 comentario
Dave B
Dave B el 18 de Ag. de 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.-',...
'MarkerIndices',2,'MarkerSize',15);
axis([-2 2 -2 2 -2 2])

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 18 de Ag. de 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

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by