MATLAB Examples

Using the layout.ScrollableGridBagLayout object.

This is a simple example using the layout.GridBagLayout object to layout a simple figure with widgets. Resize layout is maintained automatically.


Creating the widgets.

Create the figure, panel and widgets.

f = figure('menubar','none', 'Position', [100 100 400 250]);
movegui(f, 'center');

% Create a panel for clear screenshots.
p = uipanel('Parent', f, 'Units', 'pixels', 'Position', [20 20 200 150]);

for indx = 1:10
    label(indx) = uicontrol('parent',p,'style','text','string',sprintf('Item Name (%d):', indx));
    edit(indx)  = uicontrol('parent', p, 'style', 'edit');

All of the widgets are placed on top of each other because of their default position values. The layout manager will handle this for us.

Creating the LayoutManager

We create a layout manager on the uipanel 'p', but we could also create one on the figure 'f'.

hl = matlabshared.application.layout.ScrollableGridBagLayout(p, 'HorizontalGap', 5, 'VerticalGap', 5);

Adding widgets

Here we add the widgets one at a time to see the layout manager work.

for indx = 1:10
    hl.add(label(indx), indx, 1, 'MinimumWidth', 80, 'Anchor', 'NorthWest');
    hl.add(edit(indx),  indx, 2, 'MinimumWidth', 40, 'Fill', 'Horizontal');