How to create points in a matlab plot from cursor position click

10 次查看(过去 30 天)
The usual procedure is to have some vectors X and Y and plot them with the function plot(X,Y) to obtain a curve.
But I want something opposite. I want to put the cursor on an XY plane coordinate that is not part of the function and have the point (xi,yi) generated from the position where I put the cursor and click.
I am not talking about using "Create Datatips" or the "Data Cursor" button but to create points outside the given curve.

回答(1 个)

Dave B
Dave B 2021-11-7
编辑:Dave B 2021-11-7
What you're describing is a callback function that adds a point, here are the ingredients you'll need:
  • a callback function: something to detect that you've clicked and give you a place to write code. ButtonDownFcn on the axes will do the trick
  • a way to get the place where the click happened, the CurrentPoint property of axes will work for this.
  • plot!
ax=gca;
plot(rand(1,10)); % just plotting something, but totally not required
hold(ax, 'on'); % just turning on hold because I'll assume that you don't want the axes reset each time you plot another point
ax.ButtonDownFcn = @(~,~)plot(ax,ax.CurrentPoint(1,1),ax.CurrentPoint(1,2),'ko');
Tiny bit of warning, you might get into trouble if you have something else in your axes, like an image or another plot that grabs the click. The HitTest property can be useful here - this documentation page has more info: https://www.mathworks.com/help/matlab/creating_plots/capturing-mouse-clicks.html

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by