File Exchange

image thumbnail


version (12.5 KB) by Mihail Petrov
An intuitive and flexible layout manager


Updated 28 Mar 2012

View License

GridLayout was inspired by the GridBagLayout manager. It is equally flexible but more intuitive for my taste and it also requires less boilerplate code.

Each cell is a uicontainer and can hold only one child. Unlike GridBagLayout, children are added at creation time by passing the cell container handle as their parent. No 'Add' methods are needed, which reduces the amount of code.

Cell sizes can be specified in pixels or as relative values. Merging and removing cells are supported, as well as various alignment options: stretch, center, left, right, top, bottom.

The archive also includes demos illustrating the GridLayout capabilities . Bug reports and suggestion for improvement are very much appreciated. Download, unpack, and enjoy!

Comments and Ratings (10)

Eric, the Layout.Cell property itself is an array of uicontainers. This is one of the the main features of GridLayout. You don't need any additional function.

To make it easier to wrap child objects, such as nested GridLayout's and plots, I think a function such as GridLayout.WrapCell(Row, Col) would help. It would create a uicontainer() inside the cell, and then return a handle to that container.

Another change was to replace the HMargin and VMargin parameters with LMargin, RMargin, TMargin, and BMargin, that is, left, right, top, and bottom. This is very useful for axes in particular. See demo 7 for an example.


Sounds awesome, can't wait to give it a try! Thanks Mihail!

Hi David & Eric,

Give me a few days still and I'll upload the fixed version. I've been really busy these days...

Thanks for your feedback!


Pretty cool library, thanks! I'm having problem #2 mentioned above by Eric Cousineau. Really needs to be able to handle multiple children in an axes object to be functional for plotting purposes.

Sure thing! I commented a small portion in the constructor to allow ~ishghandle() objects to contain a GridLayout.
I also updated the UpdateLayout() method to allow for plots.
I have uploaded a diff file:

However, I think it would probably be easier if both plots and child GridLayouts were wrapped in uicontainers() - that way they can be contained by a GridLayout. I was thinking that this wrapping could happen at the first call to UpdateLayout().

Eric, thanks for the feedback and rating.

Could you indicate what exactly you changed in the code? In the past months I made several small changes myself and I'd like to merge them with yours before uploading a new version.

Just realized a couple of things:
1) You can nest GridLayout's by disabling the check (ishghandle()) in the constructor and wrap it in a uicontainer.
2) If you add legends for your plot, it will introduce additional children into the cell, causing errors. The best fix for this is to wrap it in a container. This is also another bugfix towards adding plots without the little workaround.

This is an awesome redux of GridBagLayout!

I noticed that there was a bug when resizing plots. Found a bugfix in Jason's layout.GridBagLayout and incorporated it into GridLayout.ResizeCell().

I have uploaded the code to

Thank you again!


Forgotten to add ParseArgs and GetArg functions to the release.

Added support for:

1) axes with legends (as requested)

2) nested layouts (as requested)

3) automatic row/column sizing (experimental)

A few bugs were fixed and an additional demo added.

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: GridBagLayout