plot inside a callback

2 ビュー (過去 30 日間)
Dario Mangoni
Dario Mangoni 2016 年 12 月 7 日
編集済み: Walter Roberson 2016 年 12 月 8 日
  1. I started a blank GUIDE app
  2. Created an axes.
  3. Created a callback on ButtonDownFcn.
  4. Just added plot([0 rand], [0 rand])
function axes1_ButtonDownFcn(hObject, eventdata, handles)
plot([0 rand], [0 rand])
Then the callback is actually called only the first time I click on the axes. This happened only with plot.
Is plot hiding the axes away?
I tried also with:
plot(handles.axes1, [0 rand], [0 rand])
or with
axes(handles.axes1)
plot(handles.axes1, [0 rand], [0 rand])
but this didn't solve the problem

回答 (2 件)

David Barry
David Barry 2016 年 12 月 7 日
I think you need to reset the callback if you plot into the axes. Try adding the line below after your plot command.
hObject.ButtonDownFcn = {@axes1_ButtonDownFcn, handles};
  3 件のコメント
David Barry
David Barry 2016 年 12 月 7 日
編集済み: David Barry 2016 年 12 月 7 日
I believe it is needed. See example below where callback is reset in 2nd section. It's nothing to do with HitTest. The alternative is to set the NextPlot property on the axes to 'replacechildren' instead of default 'replace'
%%all good
a = gca;
plot(a, 1:10);
a.ButtonDownFcn = @(~,~) disp('You clicked');
a.ButtonDownFcn
%%now plot some new data and see callback is empty
plot(a, 1:20);
a.ButtonDownFcn
Walter Roberson
Walter Roberson 2016 年 12 月 7 日
Ah, I see what you mean. But instead,
a.NextPlot = 'replacechildren';

サインインしてコメントする。


Walter Roberson
Walter Roberson 2016 年 12 月 7 日
Once the axes has something in it, then your clicks are potentially being absorbed by what is plotted in the axes rather than getting through to the axes itself.
If you are using R2014b or later, see https://www.mathworks.com/help/matlab/creating_plots/capturing-mouse-clicks.html with regards to pickableparts and hittest .
In R2014a and earlier you can still look at that, but only the hittest part existed then.
  2 件のコメント
David Barry
David Barry 2016 年 12 月 7 日
編集済み: David Barry 2016 年 12 月 7 日
Incorrect. Issue is that callback is getting reset because default value for the NextPlot property on the axes is 'replace'.
Walter Roberson
Walter Roberson 2016 年 12 月 8 日
編集済み: Walter Roberson 2016 年 12 月 8 日
Both issues are important when you are dealing with axes callbacks.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by