Allows fully utilizing the figure surface by defining margins, by wrapping the existing subplot function. Unfortunately the original subplots function spends lavishly the figure area , without allowing the user to control it in a convenient manner. In addition to subplot functionality (fully supported) the user can define margins, which result in larger axis presented in the figure. Default margins are 1% of figure area.
Thanks goes to a colleague- Adi N., who both urged me to write this function down, and assisted in writing it.
Hope you will find this one useful.
Some subplot functionality s now supported. However, I must mention this looks quiet buggy to me. Check this out:
subplot(1,2,1,'Position',[0.1,0.1,0.3,0.3]); % 'Position' is ignored!!!
Nikolay S. (2020). Controllable tight subplot (https://www.mathworks.com/matlabcentral/fileexchange/30884-controllable-tight-subplot), MATLAB Central File Exchange. Retrieved .
Thank you very much, just what I was looking for
Excellent function! Very useful when handing numerous figures.
Great syntax and it just works!
Space for labels should be somehow automatically included. You have to redefine margins for different size of the figure.
Hi Neil Tandon.
Thanks for the flattering grade. I am personally using Windows "Sniping Tool" to export relevant figure region or alternatively Matlab save figure to apply to multiple figures.
Works great for my needs. A good companion to this is the export_fig package, which makes it easy to crop out any whitespace on the figure edges. (I saw other comments about wanting to do this.)
Good morning Varoujan.
You're correct. I will add this change soon :).
A minor correction to the demo script - for it to work correctly, replace the line:
[currDir, ~, ~] = fileparts(which(mfilename));
dir_file = dir(fullfile(currDir,'*.jpg'));
Without this change, the demo script looks at the current working directory and NOT the directory where the demo_subplot_tight.m file is. With the change above, one gets the location of the .m file and uses it in the dir command.
I love that it can replace the built-in subplot with minor modification in the caller script. I wish it could handle margins in addition to gaps though.
First of all, there is no need for repeating your message. It is perfectly clear when written once.
To the point- your script's issue can be solved by setting the figure size to the desired dimentions. Either repalce the figure command with : "figure('Position', [0,0,240,291*2]);",
or add :
after your script.
I trust you will know where do the figure "position" parameters come from...
Regarding "subplot_tight" functionality as I see it. subplot_tight gola is to devide the figure to subplots spaced by margins specified by the user. Unfortunately, in case of image data (and some other cases) I have no way of knowing in advance what are the appropriate figure dimensions (as plotting commands follow subplot_tight). This will work differently with plot commands btw.
To see for yourself run the following command "subplot_tight(1,2,1, [0,0])" and verify that the axis "subplot_tight" generates have no (0) margins. The following "imshow" command in your script, actually places into the axis a plot with smaller dimensions, which results in margins you're so unhappy about.
So at this point setting appropriate figure size is under users responsibility. Actually it is not too hard to deal with, but unfortunately I found no way to do it inside subplot_tight scope.
Hope I've unswered your question.
Could you comment on why the script below using your code does not work ? MATLAB displays extra vertical space even though I asked for empty margins:
I = imread('pout.tif');
margins = [0 0];
Could you comment why the script below using your code does not work ? MATLAB displays extra vertical space even though I asked for empty margins:
I = imread('pout.tif');
margins = [0 0];
Yes, that's right. This is how subplot.m works - it creates new axes on a figure, but it also simply sets existing axes as current, if the same mnp value is given. I'm not sure how subplot accomplishes this, but using tags is a quick solution that will work most of the time.
Thank you for your comment and rating. Could you explain in more detail the feature you want to be added? Assuming subplot is called with [m,n,p] do you want to take care of the case where subplot_tight is called with same m,n,p] ? You want only the axis CData to be replaced, instead deleting current axis and creating a new one instead? Please elaborate.
Useful, concise function. It would be more useful if calling repeatedly with the same parameters did not destroy the previously-created axes (this is how subplot behaves). I did this by tagging the subplot_tight axes, then checking if an axes with that tag already exists on the current figure.
Updated demo code, using Varoujan good advice.
A bit shorter implementation, now supporting functionality Allan has requested.
.png replaced by .jpg's, to reduce file size, screen-shot modified.