Determine if mouse click on a plot is within plot region

15 ビュー (過去 30 日間)
Luke Chen
Luke Chen 2011 年 4 月 5 日
Hi, so I have a 2D plot, and when I click on any point, I need to know whether the point I clicked is inside the plot region or outside. Can someone show me how it can be done? Thanks
  1 件のコメント
Luke Chen
Luke Chen 2011 年 4 月 5 日
Sorry, the title should say "click" instead of "clock"

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

採用された回答

Jiro Doke
Jiro Doke 2011 年 4 月 5 日
What do you mean by "plot region"? Within the X-limit of the axes? Within the plotted data range?
Take a look at ginput. The output of it can be used to compare with xlim and ylim, or the plotted data range (depending on what you mean by "plot region").

その他の回答 (2 件)

Matt Fig
Matt Fig 2011 年 4 月 5 日
Here is a way to do it programmatically, as in a GUI for example. Save this in an M-file then run the M-file. Click on the axes or outside the axes:
function [] = example()
fh = figure;
axes('units','pix')
plot(1:10)
set(fh,'windowbuttondownfcn',{@fhbdfcn,get(gca,'position')})
function [] = fhbdfcn(varargin)
% WindowButtonDownFcn for figure.
P = varargin{3};
cp = get(varargin{1},'currentpoint');
if cp(1)>P(1) && cp(2)>P(2) && cp(1) < (P(1)+P(3)) && cp(2)<(P(2)+P(4))
disp('In Axes')
else
disp('Not in Axes')
end

Luke Chen
Luke Chen 2011 年 4 月 5 日
Yup, exactly what I needed. I just needed to make sure ginup was within the limits of the x and y axes.
Thanks for the quick reply!

カテゴリ

Help Center および File ExchangeData Exploration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by