ButtonDownFcn does not work on a plot
조회 수: 6 (최근 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!