Tuesday, May 17, 2011

Matlab Subplot Spacing

Hey all, Just a quick matlab tip. I have been playing around with sub plots quite a bit and the margins between images seems far too big, especially when you are subplotting quite a few figures. I did a bit of a google, but couldn't find anyone with a quick tutorial on how to reduce the margins and increase image size. So here it is:

1) Go to your MATLAB folder in program files. It was 'C:\Program Files\MATLAB\' for me.
2) Navigate to '...\MATLAB\toolbox\matlab\graph2d\subplot.m'.
3) Make a copy of this subplot.m and put it in your current working directory (wherever your current files are).
4) Open it up, and find the line that says 'inset = [.2 .18 .04 .1]; % [left bottom right top]'. Change this to whichever margins you are after. I use 'inset = [.02 .05 .02 .05];'.
5) Save it.

If you want a more permanent solution, replace the file at '...\MATLAB\toolbox\matlab\graph2d\subplot.m' with the fixed subplot.m file, so that the fix will apply to all future projects. Don't forget to make a backup of the original file in case something goes wrong. I have checked this with R2010a and R2010b. If any changes needed for other versions, post a comment and let me know.

Now, whenever you make subplots in this folder, the margins will be smaller and the plots with be bigger! Note that this still isn't perfect, as the margins can still be pretty big when you have elongated graphs, but to fix that you have to start playing around with graph location and size, which is a bit more trial and error.