How to get pointer location as grid point of an axes in matlab guide?

조회 수: 16 (최근 30일)
In MATLAB GUIDE, i have a application to place a imgae object on the exact grid point loaction of an axes.
I have a solution to place the object on random loaction on the axes through getting the pointer location from the command " get(0,'PointerLocation')".
For the current application, the pointer location need to be exact grid point of the axes.
Suggest me a solution.
  댓글 수: 5
Vinothkumar Sethurasu
Vinothkumar Sethurasu 2021년 5월 20일
편집: Jan 2021년 5월 21일
>>The grid mesh prepared on create function with these commands.
xlim([0 1]);
ylim([0 0.5]);
set(gca,'Xtick', 0:0.01:1);
set(gca,'Ytick', 0:0.0355:0.5);
>> The blue star is the image object need to be placed on the grid point. the image object is created by buttondown function with these commands,
handles.componentAxis(end)=axes('parent',handles.Sketch_window, ...
'position',[alpha-0.05 beta-0.04 0.1 0.1]);
matlabImage = imread('D:\Vinoth\star.png');
the alpha & beta values will be calculated from getting the pointer location from - get(0,'PointerLocation') command.
Now, i need to pick the exact grid intersection point to place the star image in a particular interval from the pre-specified grid mesh.
Vinothkumar Sethurasu
Vinothkumar Sethurasu 2021년 5월 21일
Hello jan,
Got my explanations?
Is it understandable now?

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

채택된 답변

Jan
Jan 2021년 5월 21일
편집: Jan 2021년 5월 21일
It is hard to convert the global mouse position on the screen to a position in the axes. Therefore get(0,'PointerLocation') is not useful. The axes contain a 'CurrentPoint' property already, which can be used directly.
I do not understand, why or where you create new axes objects.
what about this example:
function [] = kkkj
AxesH = axes('XLim', [0 1], 'YLim', [0 0.5], ...
'Xtick', 0:0.01:1, 'Ytick', 0:0.0355:0.5, ...
'XGrid', 'on', 'YGrid', 'on', ...
'NextPlot', 'add', ... % as: hold on
'ButtonDownFcn', @myAxesClick);
end
function myAxesClick(AxesH, EventData)
P = get(AxesH, 'CurrentPoint'); % Mouse click position in Axes
XTick = get(AxesH, 'XTick'); % X and Y grid
YTick = get(AxesH, 'YTick');
[~, xm] = min(abs(P(1, 1) - XTick)); % Find nearest X and Y grid point
X = XTick(xm);
[~, ym] = min(abs(P(1, 2) - YTick));
Y = YTick(ym);
plot(X, Y, 'pb', 'MarkerSize', 20); % Plot a star
end
  댓글 수: 3
Jan
Jan 2021년 5월 21일
interp1 is not implemented efficiently. This is not a problem, if the code involves a GUI interaction with a user, because waiting for the mouse click is still the bottleneck.
Vinothkumar Sethurasu
Vinothkumar Sethurasu 2021년 5월 24일
Thanks a lot.
Its working well for my application.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by