Does anyone know how I can get the current position of my cursor when I move over my figure with my cursor?

This position should be relative to the lower left corner of the figure and the unit should be pixels (to be able to define the position of the axes for example).
How to get the current pointer/mouse position in pixels relative to the lower left corner of the parent container (the red cross should have pixel coordinates [0,0])?
Orange = imread('77.jpg');
figure
imshow(Orange)

Answers (2)

cp = get(gcf(),'CurrentPoint');
display(cp);

1 Comment

It doesn't seem to work. When I run it, I get cp = [0 0] and if I click on a different place in the parent container nothing happens. Why?

Sign in to comment.

Use impixelinfo:
Orange = imread('77.jpg');
imshow(Orange)
axis('on', 'image')
impixelinfo

11 Comments

This is not what I am seeking for. I don't want to know the pixel coordinates compared to the axes, but compared to the parent container. The red cross should have pixel coordinates of [0,0]. So I also want to know the position outside the axes, for example for GUI items I want to place.
To be able to use the 'Position' property if you use axes() or uicontrol() for example. So to know the position in order to place the axes() easily.
Just set the figure's units to 'normalized' and then use trial and error to position the controls where you want.
Orange = imread('peppers.png');
imshow(Orange)
% axis('on', 'image')
impixelinfo
g = gcf;
g.Units = 'normalized';
% Maximize the window.
g.WindowState = 'maximized'
drawnow;
% Get handle to axes control on the window.
ax = gca
% Make it in the lower left
xLeft = 0.0; % 0% of the way from left to right.
yBottom = 0.10; % 10% of the way up from the bottom.
width = 0.5; % Axes control is half the width of the figure.
height = 0.35; % Axes control is 35% the height of the figure.
% Actually width and height don't seem to matter for an image.
ax.OuterPosition = [xLeft, yBottom, width, height];
Thanks, however that's what I try to avoid ''error and trial''. That's why I prefer to know how to know the position from the lower left corner of the parent container. It should be possible I guess. Maybe I asked the question in a wrong way or used the wrong tags.
For any axes or uipanel(), set the objects Units to 'pixels', and then the first two elements of its Position property will be the lower left corner relative to the parent container.
If you are not using uipanel() then it will be the position relative to the parent figure.
If you are using uipanel() then you might need to climb the hierarchy of parents finding the Position each time in order to calculate the position relative to the figure. However... if you are trying to position a UI element, you should almost always be positioning it relative to its direct container rather than the figure.
What you are saying is correct. However, I want to first know what the pixel coordinates are relative to the parent container, so that I can place the axes() and uicontrol() items easily and fill in the first two elements of the Position property. If you use the axis() or uicontrol() command, the items are placed for both commands relative to the lower left corner of the parent container, as I showed in the figure in my question with a red cross.
The question is now, however, when I move my mouse over the parent container, I want to see the pixel coordinates relative to the parent container i.e. where the red cross is, so I can use the Position property of the axes() and uicontrol() commands without error and trial. That's what I want to know, as it will save me quite some time now and in the future.
Let me know if it is still unclear what I want to achieve.
I'm not aware of a way to do that with the main figure. No "live" reporting with a convenient function. I only know of a way to do that (with impixelinfo) over an image in an axes on a figure. However you should still be able to achieve what you want with some trial and error placement of your control on the figure. It's only something you need to do once, when designing your figure layout, so it's not a huge time suck.
That said, I think you could perhaps setup a mouse movement callback function and maybe put some code in there to get the current mouse position and report it to a static text label on your figure. It's a bit tricky and will take you longer than a trial and error method, though you may learn something new while doing it. Like I said, there's no easy and convenient way to do it, like with a single function call (like impixelinfo), that I'm personally aware of.
I had a break, so sorry for my delayed response. Thanks for sharing your knowledge and it's a pitty there's is probably no easy and convenient way to do it. Otherwise I probably I already would have the answer by now.
In all the years I've used MATLAB I've never had to mouse around outside an axes and get the (x,y) coordinates. I've only needed the coordinates inside the image itself.
You can get the 'Position' property for all the controls on the figure window, and then dynamically place things. Like if you want a scroll bar halfway between the left of the figure and the left of the axes you can do
pos = handles.axes1.InnerPosition;
pos2 = handles.listbox1.Position;
pos2(1) = pos(1)/2;
handles.listbox1.Position = pos2;
The only thing different than knowing the x value after interactively mousing around and finding it, is line 3 where it's computed relative to other things on the figure window.
Thanks for the answer.
I used now this:
while true
get(0, 'PointerLocation')
pause(0.01);
end
It gives the location compared to the lower left corner of my computer window. With a bit head calculation, I could place everything.

Sign in to comment.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Asked:

S.
on 17 Dec 2021

Commented:

S.
on 10 Jan 2022

Community Treasure Hunt

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

Start Hunting!