File Exchange

image thumbnail

Distribute figures

version (548 KB) by Anders Simonsen
This function can distribute figures on different parts of the screen(s).


Updated 31 Mar 2015

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Are you tired of moving your figures around to see them all? This function does it all for you! It can distribute in all parts of the screen, include Simulink figures or not and a whole lot more! It also supports secondary monitor(s) where the figures can be distributed.
>>>> A sample image can be found here: <<<<<
The function calls needed to distribute in the shown pattern above is only:


(Don't worry - it looks complicated, but by executing distFig(); the figures will also be nicely distributed. :) ) Also, there are two tutorials included to guide you through the various features.

The function supports all resolutions (also different resolutions on different monitors). It has been made using MATLAB 2015a and will thus not work properly for version 2014a and previous due to the changes in the figure handle system in version 2014b a newer releases.

The function also detects new updates automatically, but this feature is very beta, so it might contain bugs. It can easily be disabled without affecting the function, if it causes problems. Please report any errors encountered with the function. :)







distFig(...,'Screen',Value) assigns where the figures will be distributed. Synonyms for 'Screen' are 'Scr' and 'S'. Value can be:
'C' / 'Center' (Default)
'W' / 'West' / 'L' / 'Left'
'E' / 'East' / 'R' / 'Right'
'N' / 'North' / 'T' / 'Top'
'S' / 'South' / 'B' / 'Bottom'
'Main' / 'Primary'
'Ext' / 'External' / 'Secondary'

distFig(...,'Position',Value) assigns in which part of the screen the figures will be distributed. Synonyms for 'Position' are 'Pos' and 'P'. Value can be:
'C' / 'Center' (Default)
'W' / 'West' / 'L' / 'Left'
'E' / 'East' / 'R' / 'Right'
'N' / 'North' / 'T' / 'Top'
'S' / 'South' / 'B' / 'Bottom'

distFig(...,'Rows',Value) assigns how many rows the figures will be distributed on. Synonyms for 'Rows' are 'R'. Value can be:
'Auto' / -1
'Auto' indicates that it automatically calculates the number of required rows.

distFig(...,'Columns',Value) assigns how many rows the figures will be distributed on. Synonyms for 'Columns' are 'Cols' and 'C'. Value can be:
'Auto' / -1
'Auto' indicates that it automatically calculates the number of required columns.

distFig(...,'Not',Value) excludes specified figures from the distribution list. Value must be an array with the excluded figure numbers. The default value is []. The input argument 'gcf' (get current figure) can be used as well.

distFig(...,'Only',Value) does only distribute specified figures. Value must be an array with the figure which will be distributed. The default value is []. The input argument 'gcf' (get current figure) can be used as well.

The remaining features are described in the file.

Cite As

Anders Simonsen (2021). Distribute figures (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (69)

Oan de Waal

Wow, this is probably the best add on I have to Matlab! Can't believe I struggled for so long without it. It's a must-have. It works seamlessly with Matlab 2020a on Windows 10 as of 13 July 2020.
Highly recommended!


Love this function! It would be great if there was an option to keep the original figure size and just ditribute the figures without resizing them.


Please ignore my previous comment. There is no compatibility issue with MATLAB 2020a. I found out the cause.


The codes do not work with MATLAB 2020a Prerelease version.

When I run the script 'Sample', there is an error as follows.
459 Fig_Number(Temp(Index)) = -(numel(Index):(-1):1);
Index exceeds the number of array elements (1).

Error in distFig (line 459)
Fig_Number(Temp(Index)) = -(numel(Index):(-1):1);

Error in Sample (line 28)

Temp =


K>> Index

Index =

3 4 2 1 9 6 5 8 7

K>> Fig_Number

Fig_Number =

14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

For earlier compatible MATLAB version 2019b, 'Fig_Number' does not include '0' and 'Temp' is empty.

K>> Fig_Number

Fig_Number =

14 13 12 11 10 9 8 7 6 5 4 3 2 1

K>> Temp

Temp =

1×0 empty double row vector


Any suggestion to fix this compatibility issue?

Chris Davey

Great flexibility, works perfectly! Thank you!


Excellent implementation! We developed a similar function without the knowledge of this code. This function is much flexible, powerful, and easier to use than our code.

David Santos

Johannes Küchle

Sören Kohnert


This is great! Thanks for sharing. Now we just need a button in the matlab editor menu or similar that can execute this script :-)

Santanu Banerjee

good one; enjoyed a lot using it.

Michael Moruzzi

Michael Moruzzi

Hi there, thanks for the function!

I am having problems using this whilst I am running a GUI. I get the following error:

Index exceeds matrix dimensions.

Error in distFig (line 459)
Fig_Number(Temp(Index)) = -(numel(Index):(-1):1);

I have tried specifying 'Only' the figures I want, but no luck. The function works fine if the GUI figure is not open.

Would be great if you could help


Brayan Torres Z.

Adam Golinski

Blagovest Mihaylov

Great Function. Tight was not performing as I expected for me. However
set(gca_Temp,'Position',[get(gca_Temp,'TightInset')*eye(4,2),1 - get(gca_Temp,'TightInset') * [1,0,1,0;0,1,0,1]']);
is perfect.
Thanks for the function.

Christian Nørgård

tahir anwar

hello friends i have to analyzed harmonic inject by ups in secondary electric distribution system and simulate in matlab anyone who can simulate secondary distribution system with ups in consumers houses >>>>>>>>> plz help me .

Anders Simonsen

@arnold: Hi. You can simply add the colorbar after you've used distFig. This will allow matlab to add the colorbar and adjust the axis, so you'll be able to see it. Like so:

Instead of this:


hi Anders,
is there a robust way to handle colorbars including their labels? 'tight = true' doesn't really to a good job of making sure the colorbars are still readable but not using tight results in too much waste in space.



Amazing! Thanks Anders.

Esben Bach-Sørensen

Nice job!

Niels Henrik Pedersen



Tom-Erik Lindberg

An option for keeping the aspect ratio of a figure fixed, while still allowing automatic resizing, would be pretty awesome.


Wonderful job definitely worth a 5-star rating!


Anders Simonsen

@arnold: Hi. Okay, I'll take a look at it in the next update. I've encountered some bugs with the 'tight' command as well, so I'll make sure it's working properly.


Hi Anders,

the 'tight' property doesn't work properly if one uses suptitle (and I use it a lot). distFig then cuts of the xlabel of plots.

kind regards


So very useful... Thank you Anders

Anders Simonsen

@Alia: I'm not supporting MATLAB 2014a and previous releases due to the changes in the graphics structure within the 2014b and newer releases. Sorry :|



I still have this error msg:
Attempt to reference field of non-structure array.

Error in distFig (line 454)
Fig_Number = {Fig_Object.Number};

Error in Sample (line 28)

I am using MatlabR2014a, how is it possible to solve it

Anders Simonsen

@Michael: Thanks! And sorry for the late answer. Sure, I'll make sure to add that to the next update, which will follow soon. :)


Hello Anders,
Impressive work! It's a very nice and handy function. Would you consider adding an option to not resize figures, but distribute them with their current size? That would help a lot.

Ke Wang Helmersson

I don't know why I got monitor matrix which didn't have an origin at [1, 1]. I had 'dbstop if error' and ran get(0,'MonitorPositions') at dbstop. I couldn't reproduce it either since there is no error.

Anders Simonsen

Okay, perfect! Also, you can see that your monitors are detected "correctly" this time. I don't know how you got your previous monitor matrix, which hadn't got an origin at [1,1].

Ke Wang Helmersson

Now my extended display works also. Thanks!

Ke Wang Helmersson

Yes, I was a bit surprised to see that I had only one row when I checked monitor. After restarting the matlab, the monitor matrix becomes:
Monitors =

-1365 1 1366 768
1 1 1920 1200

Anders Simonsen

Okay, it seems like you only have a single monitor. The number of rows in the monitor matrix is the number of monitors:

[Origin_x , Origin_y , Width , Height]

Mine yields:


Can you describe your setup?

Btw. MATLAB does not recognize changes in the monitor configuration, AFTER it has been started. So if you plug in your monitor AFTER you started MATLAB, it will not detect it - so try and restart MATLAB and see if that works.

Ke Wang Helmersson

Yes, I got a warning message. Here is the monitor matrix.
Monitors =

1 1 1920 1080

Anders Simonsen

@Chad: Thanks! :D

@Ke Wang: Yearh - you should be able to. Does you come with a warning message? Can you post your monitor matrix again (Monitors = get(0,'MonitorPositions');)? (If it has changed?)

You can see my setup here, where I made a brief description of how to distribute to external monitor(s).

Ke Wang Helmersson

Hello Anders, I wonder if I can distribute figures to extended displays. It seems that the function doesn't consider it as external monitor and couldn't find it.

Chad Greene

That is the finest FEX submission icon I have ever seen. Well done.


Anders Simonsen

@Ke Wang: Thanks a lot! :D I thought it might help with some of the features. :) I plan on making more tutorials soon.

Ke Wang Helmersson

It works very well now. Thanks for the function!

Ke Wang Helmersson

And the tutorial is great!

Anders Simonsen

@Ke Wang: Hmm - that's weird. Usually the primary monitor has origin in [1,1], but yours seem to be located at [1,121]. I couldn't reproduce your settings, as I instead got:

-1365 153 1366 768
1 1 1920 1080

But I've just uploaded a new version, where the primary monitor is detected by minimizing the absolute sum of the two first columns, so it should work for you now, if you just download the latest one. :)

Ke Wang Helmersson

Thanks for your quick reply. Here is the matrix:
Monitors =

-1365 273 1366 768
1 121 1920 1080


Anders Simonsen

@Ke Wang: All right - I don't know what causes that. Can you evaluate the following in your Matlab workspace and send me the resulting matrix:

Monitors = get(0,'MonitorPositions');

Monitors is a (n x 4) matrix, where n is the number of monitors you have.

Ke Wang Helmersson

I have tried Sample.m in MATLAB2014b and got following error:
Warning: Screen 'C' could not be found!
> In distFig at 382
In Sample at 21
Index exceeds matrix dimensions.

Error in distFig (line 408)
x = round(linspace(Limit{2}(1),Limit{2}(2),Cols + 1) * Monitor(3)) + 1;

Error in Sample (line 21)
distFig('Pos','NW' , 'Only',1);

408 x = round(linspace(Limit{2}(1),Limit{2}(2),Cols + 1) * Monitor(3)) + 1;

Anders Simonsen

@Ramin: The function will not work for Matlab version 2014a and previous. This is due to the massive overhaul of the graphics handle system in 2014b and forward.
You can try and rewrite the lines that are not working, as I'll not support version 2014a and previous anymore - sorry. :)

Ramin Rajabioun

running Sample.m produces an error like this on my MATLAB 2014a:

Attempt to reference field of non-structure array.

Error in distFig (line 322)
Fig_Number = {Fig_Object.Number};

Error in Sample (line 21)
distFig('Pos','NW' , 'Only',1);

Anders Simonsen

@Mike: Thanks - so do I. ;)

Okay, thanks for the feedback and for finding and fixing the problem! :D I'll make sure to include it in the next version of it.

I've also stumbled upon a few bugs as well, which aren't working properly after the 2014b release, so I'll update it again soon.


Very nice feature, I use it all the time :)

Had a little compatibility issue with the newly released R2014b. Since figure handles are no longer numbers but figure objects, the sort feature no longer works. Hence, the figures get distributed nicely, just not in the right order. I fixed this by accessing the Number property of the figure objects instead. (see below)

My code adaptation, first line of 'Generate figure list' section:
Fig_List = findall(0,'type','figure');

Fig_Object = findall(0,'type','figure');
Fig_List = [Fig_Object.Number];

This way, Fig_List is a vector of numbers again that can be sorted a few lines below ;) Not sure if it's the neatest way, but works fine for me :)

Anders Simonsen

@arnold: Can you send me an example code, where you are having the problem? Then I can take a look at it. :) (Btw. I've sent you a private message as well)


hi anders.
I've integrated the 'tight' argument in many of my functions but just saw that it is cutting off any xlabel one adds to a boxplot... no idea why xlabels of boxplots are treated entirely different than those of other plots.

Anders Simonsen

@arnold: I've just updated it again and now it should work. I didn't test it yesterday on 2014a as I didn't have acces to it, but now I have done that and fixed the error.

It is caused by the new way that the figure system is made in 2014b, which has been changed a lot! The only thing you should do is to download it again or just delete the line that is causing the error. :)


@ Anders: thanks for the update. Unfortunately the function does not work anymore, no matter what type of figures are open. i.e. opening four figures, no content, then executing 'distFig' with no arguments. Gives the following error on 2014a:

Improper index matrix reference.

Error in distFig>@(n)(Figs(n).Number) (line 199)
Figs = sort(arrayfun(@(n) (Figs(n).Number),1:numel(Figs)));

Error in distFig (line 199)
Figs = sort(arrayfun(@(n) (Figs(n).Number),1:numel(Figs)));

Anders Simonsen

@arnold: I've just updated the function to support the other function. The problem was that "fig()"; changed 'Units' to 'centimeters', whereas my function was using 'pixels'. Furthermore, I've added a new feature called distFig(...,'Tight',true/false), which can remove the whitespace around the figure like "fig();" - if that is what you're looking for. :)

And thanks for the feedback. :)


for some reason 'distFig' can't deal with figures produced by 'fig' from the fileExchange, which is also a great tool:

The figures disappear from the screen even though they're still opened.


Often i used to be both tired and depressed - but then I tried Quack Simonsen's mircale function. Now I feel much better - thanks Simonsen!

Anders Simonsen

@Ivan: Sorry for the late response. Yes - the new function does support different screen resolutions. :)


Does it support different screen resolutions? Or just 1920*1080?

Exactly what I was looking for and easy to use.

Tobias Olesen

Great funktion!



Anders Simonsen

Thanks. So I assume you would have preferred something like distFig('Position','Left','Screen','Right');?


Nice function. Although I think it doesn't make any sense to accept multiple character input like 'r','l' since only the last input is used by the function.

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

Community Treasure Hunt

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

Start Hunting!