File Exchange

image thumbnail


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

1 Download

Updated 28 Mar 2012

View Version History

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!

Cite As

Mihail Petrov (2021). GridLayout (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)

Mihail Petrov

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.

Eric Cousineau

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.

Mihail Petrov

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!

Mihail Petrov

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.

Eric Cousineau

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().

Mihail Petrov

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.

Eric Cousineau

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.

Eric Cousineau

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!

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

Inspired by: GridBagLayout


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!