Get component position in pixels
Use this function only with GUIDE, or with apps created using the
figure
function.
position = getpixelposition(handle)
position = getpixelposition(handle,recursive)
position = getpixelposition(handle)
gets the position, in pixels, of the component
specified by handle
. MATLAB® returns the position
as a four-element vector that
specifies the location and size of the component: [distance from left, distance from
bottom, width, height].
position = getpixelposition(handle,recursive)
gets the position as
above. If recursive
is true, the returned position is relative to the
parent figure of handle
.
Use the getpixelposition
function only to obtain coordinates for
children of figures created with the figure
function, or for child containers of the figure (such as panels or
button groups). Results are not reliable for children of axes or other graphics
objects.
This example creates a push button within a panel, and then retrieves its position, in pixels, relative to the panel.
f = figure('Position',[300 300 300 200]); p = uipanel('Position',[.2 .2 .6 .6]); h1 = uicontrol(p,'Style','PushButton',... 'Units','Normalized',... 'String','Push Button',... 'Position',[.1 .1 .5 .2]); drawnow; pos1 = getpixelposition(h1)
pos1 = 18.6000 12.6000 88.0000 23.2000
The following statement retrieves the position of the push button, in pixels, relative to the figure.
pos1 = getpixelposition(h1,true)
pos1 = 78.6000 52.6000 88.0000 23.2000