Documentation Center

  • Trial Software
  • Product Updates

Positioning Figures

Introduction

The figure Position property controls the size and location of the figure window on the screen. Monitor screen size is a property of the root Handle Graphics® object. At startup, the MATLAB® software determines the size of your computer screen and defines a default value for Position. This default creates figures about one-quarter of the screen's minimum extent and places them centered left to right, in the top half of the screen.

The Position Vector

MATLAB defines the figure Position property as a vector.

[left bottom width height]

left and bottom define the position of the first addressable pixel in the lower left corner of the window, specified with respect to the lower left corner of the screen. width and height define the size of the interior of the window (i.e., exclusive of the window border).

MATLAB does not measure the window border when placing the figure; the Position property defines only the internal active area of the figure window.

Because figures are windows under the control of your computer's windowing system, you can move and resize figures as you would any other windows. MATLAB automatically updates the Position property to the new values.

Figure Position and Window Managers

Your computer's window manager controls the layout of windows on monitors and on virtual desktops. It might not honor a request to place a figure window that would cause the entire figure or its top border to be located off-screen. A window manager also might force windows to have a certain minimum or maximum width or height. Such actions can cause a figure's position to differ from what you specify, and results can vary across platforms and window managers.

Figure Position for Docked Figures

When a figure is docked in the MATLAB desktop, the Position property is defined with respect to the figure group container within the desktop. See Docking Figures in the Desktop for more information.

Units

The figure's Units property determines the units of the values with which you specify its position on the screen. Possible values for the Units property are

set(gcf,'Units')
[ inches | centimeters | normalized | points | {pixels} | 
characters]

with pixels being the default. These choices allow you to specify the figure size and location in absolute units (such as inches) if you want the window always to be a certain size, or in units relative to the screen size (such as pixels).

Characters are units that enable you to define the location and size of the figure in units that are based on the size of the default system font.

Determining Screen Size

Whatever units you use, it is important to know the extent of the screen in those units. You can obtain this information from the root ScreenSize property. For example:

get(0,'ScreenSize')
ans =
    1 1 1152 900

In this case, the screen is 1152 by 900 pixels. MATLAB returns the ScreenSize in the units determined by the root Units property. For example,

set(0,'Units','normalized')

normalizes the values returned by ScreenSize.

get(0,'ScreenSize')
ans =
    0 0 1 1

MATLAB determines the screen size in absolute units (e.g., inches) by dividing the number of pixels in width and height by the screen DPI (see the ScreenPixelsPerInch property). This value is approximate and might not represent the actual size of the screen.

Defining the figure Position in terms of the ScreenSize in normalized units makes the specification independent of variations in screen size. This is useful if you are writing a MATLAB file to use on different computer systems. It does, however, result in differently-shaped figures on monitors having different aspect ratios.

The ScreenSize property is static. Its values are read only at MATLAB startup and not updated if system display settings change. Also, the values returned might not represent the usable screen size for application developers due to the presence of other GUIs, such as the Microsoft® Windows® taskbar.

Example — Specifying Figure Position

Suppose you want to define two figure windows that occupy the upper third of the computer screen (e.g., one for uicontrols and the other for displaying data). To position the windows precisely, you must take into account the window borders (which can include a menu bar and toolbars) when calculating the size and offsets. For this purpose, use the windows' OuterPosition rather than their Position property.

  1. Ensure root units are pixels and get the size of the screen and create a figure window:

    set(0,'Units','pixels') 
    scnsize = get(0,'ScreenSize');
    fig1 = figure;
  2. The figure Position property only includes the drawable extent of the window, exclusive of the window borders. Obtain the entire window's size from the OuterPosition property, and compare the two:

    position = get(fig1,'Position')
    outerpos = get(fig1,'OuterPosition')
    borders = outerpos - position
    
    position =
       560   528   560   420
    
    outerpos =
       556   524   568   495
    
    borders =
        -4    -4     8    75

    The left, right, and bottom borders are each 4 pixels wide. The top border, which contains a menu bar and a figure toolbar is 75-4, or 71 pixels wide.

  3. Create a second figure, which defaults to the same size as the first one:

    fig2 = figure;
  4. Define the desired size and location of the figures. Leave a space equal to their border width between them:

    edge = -borders(1)/2;
    pos1 = [edge,...
            scnsize(4) * (2/3),...
            scnsize(3)/2 - edge,...
            scnsize(4)/3];
    pos2 = [scnsize(3)/2 + edge,...
            pos1(2),...
            pos1(3),...
            pos1(4)];
  5. Reposition the two figures by changing both of their OuterPosition properties:

    set(fig1,'OuterPosition',pos1) 
    set(fig2,'OuterPosition',pos2)
    

The two figures now occupy the top third of the screen.

Was this topic helpful?