Set figure coordinates by clicking on it
11 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (2 件)
Benjamin Kraus
2023 年 6 月 21 日
編集済み: Benjamin Kraus
2023 年 6 月 21 日
I'm pretty sure you can replace the word capturepoints in your code with ginput and get the behavior you are looking for.
2 件のコメント
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:
- change the axes units to pixels
- use tightPosition to get the pixel position of the axes plot box
- divide by the xlim and ylim to get a conversion factor between pixels and data
- 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
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!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Exploration についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!