Main Content

setpixelposition

Set component position in pixels

    Description

    example

    Note

    The setpixelposition function is not supported in App Designer or in apps created using the uifigure function. Instead, to specify the position of a component in pixels, set its Units property to 'pixels', and then set its position using the Position property.

    pos = setpixelposition(c,position) sets the position in pixels of the component specified by c. Specify the position as a four-element vector that gives the location and size of the component relative to its parent container in the form [left bottom width height].

    example

    pos = setpixelposition(c,position,isrecursive), where isrecursive is true, sets the component position relative to the parent figure. The default for isrecursive is false, which sets the position relative to its immediate parent container.

    Examples

    collapse all

    Create a push button within a panel container.

    f = figure('Position',[300 300 300 200]);
    p = uipanel('Position',[.2 .2 .6 .6]);
    btn = uicontrol(p,'Style','PushButton', ...
         'Units','Normalized', ...
         'String','Push Button', ...
         'Position',[.1 .1 .5 .2]);

    Get the position in pixels of the push button with respect to the panel. Update the position vector by moving it 10 pixels right and up, and by increasing the width and height by 25 pixels. Use the updated position vector to set the position of the push button within the panel.

    pos = getpixelposition(btn);
    newpos = pos + [10 10 25 25];
    setpixelposition(btn,newpos);

    Update the position of a push button relative to the figure window to maintain the position of the button after moving its parent container.

    Create a push button within a panel container.

    f = figure('Position',[300 300 300 200]);
    p = uipanel('Position',[.2 .2 .6 .6]);
    btn = uicontrol(p,'Style','PushButton', ...
         'Units','Normalized', ...
         'String','Push Button', ...
         'Position',[.1 .1 .5 .2]);

    Get the position of the push button, in pixels, with respect to the figure window and store it in pos.

    pos = getpixelposition(btn,true)
    pos =
    
       80.6000   54.6000   88.0000   23.2000

    Update the position of the panel. Since the position of the push button is relative to its parent, the panel, this changes the position of the push button as well.

    p.Position = [.1 .1 .8 .8];

    Set the position of the push button relative to the figure window to reset the button to its original location while maintaining the updated panel position.

    setpixelposition(btn,pos,true);

    Input Arguments

    collapse all

    UI component.

    New position of the component in pixels, specified as a vector of the form [left bottom width height]. The left and bottom elements of the vector are relative to the immediate parent container or to the parent figure, depending on the value of isrecursive. This table describes each element in the vector.

    ElementDescription
    leftDistance from the inner left edge of the parent to the outer left edge of the UI component
    bottomDistance from the inner bottom edge of the parent to the outer bottom edge of the UI component
    widthDistance between the right and left outer edges of the UI component
    heightDistance between the top and bottom outer edges of the UI component

    Whether to set the position relative to the parent figure, specified as a numeric or logical 0 (false) or 1 (true).

    • 0 (false) — The function sets the component position relative to its immediate parent.

    • 1 (true) — The function sets the component position relative to the parent figure window.

    More About

    collapse all

    Pixels

    Distances in pixels are independent of your system resolution on Windows® and Macintosh systems:

    • On Windows systems, MATLAB® defines a pixel as 1/96th of an inch.

    • On Macintosh systems, MATLAB defines a pixel as 1/72nd of an inch.

    On Linux® systems, your system resolution determines the size of a MATLAB pixel. For more information, see DPI-Aware Behavior in MATLAB.

    Introduced in R2007a