필터 지우기
필터 지우기

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

조회 수: 10 (최근 30일)
OSCAR BELLON-HERNANDEZ
OSCAR BELLON-HERNANDEZ 2021년 11월 6일
댓글: Edo Qi 2022년 11월 22일
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