ButtonDownFcn does not work on a plot

9 ビュー (過去 30 日間)
Filippo Mayer
Filippo Mayer 2023 年 8 月 18 日
コメント済み: Dave B 2023 年 8 月 18 日
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.

採用された回答

Dave B
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
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
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

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by