Does anyone know how I can get the current position of my cursor when I move over my figure with my cursor?
Show older comments
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)
Image Analyst
on 19 Dec 2021
Use impixelinfo:
Orange = imread('77.jpg');
imshow(Orange)
axis('on', 'image')
impixelinfo
11 Comments
S.
on 19 Dec 2021
Image Analyst
on 19 Dec 2021
WHY do you want/need to know that?
S.
on 19 Dec 2021
Image Analyst
on 19 Dec 2021
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];
S.
on 21 Dec 2021
Walter Roberson
on 21 Dec 2021
Edited: Walter Roberson
on 21 Dec 2021
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.
Image Analyst
on 21 Dec 2021
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.
S.
on 6 Jan 2022
Image Analyst
on 6 Jan 2022
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.
S.
on 10 Jan 2022
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!