View License

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

» Watch video

Highlights from
Distribute figures

5.0 | 16 ratings Rate this file 15 Downloads (last 30 days) File Size: 548 KB File ID: #37176 Version: 1.17
image thumbnail

Distribute figures



15 Jun 2012 (Updated )

This function can distribute figures on different parts of the screen(s).

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

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.

Required Products MATLAB
MATLAB release MATLAB 8.5 (R2015a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (53)
10 Jan 2017 Christian Nørgård

01 Oct 2016 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 .

Comment only
04 Sep 2016 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:

Comment only
25 Aug 2016 arnold

arnold (view profile)

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.


Comment only
06 Jun 2016 Rex

Rex (view profile)

Amazing! Thanks Anders.

Comment only
22 Dec 2015 Esben Bach-Sørensen

Nice job!

22 Dec 2015 Niels Henrik Pedersen


16 Dec 2015 grwa

grwa (view profile)

12 Nov 2015 Tom-Erik Lindberg

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

16 Oct 2015 Ben

Ben (view profile)

Wonderful job definitely worth a 5-star rating!

20 Jul 2015 Anthony

16 Jun 2015 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.

Comment only
14 Jun 2015 arnold

arnold (view profile)

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

Comment only
03 May 2015 Luke

Luke (view profile)

So very useful... Thank you Anders

07 Apr 2015 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 :|

Comment only
07 Apr 2015 Alia

Alia (view profile)


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

Comment only
10 Mar 2015 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. :)

Comment only
23 Feb 2015 Michael

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.

Comment only
19 Jan 2015 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.

Comment only
19 Jan 2015 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].

Comment only
19 Jan 2015 Ke Wang Helmersson

Now my extended display works also. Thanks!

Comment only
19 Jan 2015 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

Comment only
19 Jan 2015 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.

Comment only
19 Jan 2015 Ke Wang Helmersson

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

1 1 1920 1080

Comment only
19 Jan 2015 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).

Comment only
19 Jan 2015 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.

Comment only
18 Jan 2015 Chad Greene

Chad Greene (view profile)

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

Comment only
18 Jan 2015 Erik

Erik (view profile)

17 Jan 2015 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.

Comment only
17 Jan 2015 Ke Wang Helmersson

It works very well now. Thanks for the function!

Comment only
17 Jan 2015 Ke Wang Helmersson

And the tutorial is great!

17 Jan 2015 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. :)

Comment only
17 Jan 2015 Ke Wang Helmersson

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

-1365 273 1366 768
1 121 1920 1080


Comment only
17 Jan 2015 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.

Comment only
16 Jan 2015 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;

Comment only
07 Jan 2015 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. :)

Comment only
07 Jan 2015 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);

Comment only
22 Oct 2014 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.

Comment only
22 Oct 2014 Mike

Mike (view profile)

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 :)

07 Sep 2014 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)

Comment only
04 Sep 2014 arnold

arnold (view profile)

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.

Comment only
20 Aug 2014 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. :)

Comment only
20 Aug 2014 arnold

arnold (view profile)

@ 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)));

Comment only
19 Aug 2014 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. :)

Comment only
19 Aug 2014 arnold

arnold (view profile)

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.

Comment only
04 Jun 2014 Anders

Anders (view profile)

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!

29 May 2014 Anders Simonsen

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

Comment only
31 Mar 2014 Ivan

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

Comment only
29 Nov 2013 Mark Søndergaard Sørensen

Exactly what I was looking for and easy to use.

29 Nov 2013 Tobias Olesen

Great funktion!

27 Jul 2012 Tom

Tom (view profile)


20 Jun 2012 Anders Simonsen

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

Comment only
20 Jun 2012 Deborah

Deborah (view profile)

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.

18 Jun 2012 1.2

Removed a misplaced "nargin".

02 Jul 2012 1.3

Added the 'Rows' and 'Offset' properties and changed the input mode a more logical way.

03 Jul 2012 1.5

Added a few lines to the description.

06 May 2013 1.6

A minor update

29 May 2014 1.7

The whole function has been updated to support different screen resolutions along with other useful features.

02 Jun 2014 1.8

Updated description

19 Aug 2014 1.9

Added support for the 2014b (pre)release. Also fixed a minor problem with the 'Units' property of the current axes.

20 Aug 2014 1.10

Fixed a minor bug with the new figure handle system of 2014b.

20 Dec 2014 1.11

The function has been updated to support the new figure handle system of 2014b and 2015a (Prerelease). Minor bugs have been fixed as well.

17 Jan 2015 1.12

Added improved relative monitor detection.
Added exceptions for special function calls, which caused errors.
Fixed minor bugs.
Added automatically update feature system (First attempt of doing so, so it might cause bugs - please report these)

17 Jan 2015 1.13

Added a tutorial file.

17 Jan 2015 1.14

Fixed a minor bug causing incorrect detection of primary monitor.

18 Jan 2015 1.15

Added a second tutorial and added an 'Adjust' feature.

29 Mar 2015 1.16

Added support for 'gcf' (get current figures) when using the 'only' and 'not' features.
Added features: 'Scale', 'ScaleOrigin', 'FreezeSize' and 'Order'.
Added additional warnings if external monitor(s) cannot be found.
Minor bug fixes.

31 Mar 2015 1.17

Fixed a minor bug with the 'only' and 'not' features when using the 'gcf' input.

Contact us