ButtonDownFcn does not work on a plot
10 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Dave B
2023-8-18
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 个评论
Dave B
2023-8-18
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])
更多回答(1 个)
Walter Roberson
2023-8-18
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!