File Exchange

image thumbnail

Limit figure size

version 1.1 (3.53 KB) by

Set minimal or maximal size of a figure



View License

LimitSizeFig - Set minimum and maximum figure size
Some dialogs become ugly, if their size is getting too small. Setting a minimal size in the figure's ResizeFcn leads to strange effects, when the left side of the figure is dragged. This function calls Java to limit the figure size.
LimitSizeFig(FigH, Prop, Ext)
  FigH: Handle of a Matlab figure.
  Prop: String, command:
        'min', 'max': Set minimum or maximum extent.
        'get': Reply the limits.
        'clear': Clear both limits.
  Ext: Extent as [1 x 2] vector, minimal width and height in pixels.
  FigH = figure;
  LimitSizeFig(FigH, 'min', [200, 200])
  LimitSizeFig('max', [400, 400]) % Uses GCF
  Limit = LimitSizeFig(FigH, 'get')

Tested: Matlab 7.7, 7.8, 7.13, 8.6, HG2, WinXP/32, Win7/64
Run the unit-test uTest_LimitSizeFig to check the validity on your Matlab version.

Comments and Ratings (6)

Rob Campbell

Rob Campbell (view profile)

Nice, thanks.

Jan Simon

Jan Simon (view profile)

The method does not work in 2011b. The min and max figure sizes are set and stored in the jFrame.fHG1Client.getWindow object, but they do not affect the position. In R2009a everything was fine.

I'm not sure how this method can be supported reliably, because I do not have all Matlab versions.


Dan (view profile)

Thanks for the script it has been very useful for me.

It seems, there is a small compability issue with 2014b and the new graphics system (HG2). To access the java frame correctly, after line 99 of your code you need to add another catch for HG2:
jClient = jFrame.fHG2Client;
(see here


Dan (view profile)

Jan Simon

Jan Simon (view profile)

Stephane: Thanks for your comment. I do not have R2013a to test or fix this.
Please feel free to send me suggestions for simplifications to the email address taken from the code.


Thanks this is nice job ... unfortunatly setting max size does not work (R2013a). As a test you can try:

h = figure();
LimitSizeFig(h, 'min', [100 100]);
LimitSizeFig(h, 'max', [200 200]);

You will see that min size is ok, but that figure can still grow by any amount ;(

NB1: It's seems to be a bug in swing for 'setMaximumSize'.

NB2: I have refactored your code here to be simpler to use:



Mentioned compatibility with HG2.


Adjutsed to R2015b

MATLAB Release
MATLAB 8.6 (R2015b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video