Get component position in pixels


position = getpixelposition(handle)
position = getpixelposition(handle,recursive)


position = getpixelposition(handle) gets the position, in pixel units, of the component with handle 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 and container components (uipanels, or uibuttongroups). Results are not reliable for children of axes or other Handle 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',...
     'String','Push Button',...
     'Position',[.1 .1 .5 .2]);
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
Was this topic helpful?