필터 지우기
필터 지우기

Set figure coordinates by clicking on it

조회 수: 7 (최근 30일)
Robert
Robert 2023년 6월 21일
편집: Benjamin Kraus 2023년 6월 21일
I have a program that outputs a figure, and I currently have to manually write down each point's pixel coordinates to put it back in the program. Is it possible to be able to click on points on each figure that would then be saved to a 2d array/vector. I would also prefer to be able to delete a point if I make an error. This would then be written into a .txt file. I would like the code to work like this:
For (figures 1-5):
[x,y] = capturepoints(figure[i]);
savetotxt([x.y])
end
The program does not use a GUI.

답변 (2개)

Benjamin Kraus
Benjamin Kraus 2023년 6월 21일
편집: Benjamin Kraus 2023년 6월 21일
See if ginput does what you need. There are a few examples on the doc page.
I'm pretty sure you can replace the word capturepoints in your code with ginput and get the behavior you are looking for.
  댓글 수: 2
Robert
Robert 2023년 6월 21일
Will this work with pixels?
Benjamin Kraus
Benjamin Kraus 2023년 6월 21일
편집: Benjamin Kraus 2023년 6월 21일
The values returned by ginput are in data coordinates, not pixel coordinates. Unfortunately, it takes some effort to convert between data coordinates and pixel coordinates.
The basic idea is to:
  1. change the axes units to pixels
  2. use tightPosition to get the pixel position of the axes plot box
  3. divide by the xlim and ylim to get a conversion factor between pixels and data
  4. multiply that conversion factor by your data coordinates.
Of course, this only works in 2D. Doing this in 3D is significantly more complicated.
Here is an example:
xlim([0 10]); % Nice round numbers to test that the math is working.
ylim([0 100]); % Nice round numbers to test that the math is working.
[xdata,ydata] = ginput(4);
pointData = [xdata ydata];
ax = gca;
ax.Units = 'pixels';
pos = tightPosition(ax);
pixelsPerData = pos(3:4)./[diff(xlim) diff(ylim)];
pointPixels = pointData.*pixelsPerData;

댓글을 달려면 로그인하십시오.


Menika
Menika 2023년 6월 21일
편집: Menika 2023년 6월 21일
Hi,
You might want to read about the 'ginput' and 'writematrix' functions of MATLAB. I've attached the links to respective documentations.
Hope it helps!

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by