Effacer les filtres
Effacer les filtres

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

17 vues (au cours des 30 derniers jours)
OSCAR BELLON-HERNANDEZ
OSCAR BELLON-HERNANDEZ le 6 Nov 2021
Commenté : Edo Qi le 22 Nov 2022
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.

Réponses (1)

Dave B
Dave B le 7 Nov 2021
Modifié(e) : Dave B le 7 Nov 2021
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

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by