This m-file is an alternative to built-in saveas command. It saves your figure without the white box/margin around it.
E Akbas (2021). Get rid of the white margin in saveas output (https://www.mathworks.com/matlabcentral/fileexchange/34024-get-rid-of-the-white-margin-in-saveas-output), MATLAB Central File Exchange. Retrieved .
when i use this script to save plotyy figures.
It cut them from one side. Is there an option that i should do to resolve this problem.
OK, great! I can setup a github repository and put the file there, ans then you can fork it from there. Then, I will put the github link in the description above to let visitors of this page know about the new github repo.
Or, if you have any other suggestion,let me know.
sure, I'd be glad to.
Thanks for the feedback.
I haven't been using MATLAB for a while and it seems that I'm not going back to MATLAB. I am very happy with Python, numpy and scipy.
Would you like to continue maintaining this file? Let me know.
one of the best scripts out there for saving figures. I've tried a fair few (exportfig, export_fig, I even wrote my own). It does however seem to have a problem with being a bit too aggressive in the cropping of axes titles and legends. Here's my suggestion to fix that (starting at line 39):
htext = findall(hfig, 'type','Text');
hlegend = findall(hfig, 'type','Legend');
hall = [htext; hlegend];
my_parent = get(hall(ii),'parent');
%if this object has an Axes as a parent, it's position will be relative
%to the axes' position and limits
p_child = get(hall(ii), 'position');
p_ax = get(my_parent, 'position');
my_xlim = get(my_parent,'xlim');
my_ylim = get(my_parent,'ylim');
p(1) = p_ax(1) + (p_child(1) - my_xlim(1)) / (my_xlim(2) - my_xlim(1)) * p_ax(3);
p(2) = p_ax(2) + (p_child(2) - my_ylim(1)) / (my_ylim(2) - my_ylim(1)) * p_ax(4);
if length(p_child) == 4
p(3) = p(1) + (p_child(3) - my_xlim(1)) / (my_xlim(2) - my_xlim(1)) * p_ax(3);
p(4) = p(2) + (p_child(4) - my_ylim(1)) / (my_ylim(2) - my_ylim(1)) * p_ax(4);
%some objects only have 3 values for position: x,y,z
p(3) = max([p(1) p_ax(1) + p_ax(3)]);
p(4) = max([p(2) p_ax(2) + p_ax(4)]);
p = get(hall(ii),'position');
p(3) = p(1) + p(3);
p(4) = p(2) + p(4);
tighest_box(1) = min(tighest_box(1), p(1));
tighest_box(2) = min(tighest_box(2), p(2));
tighest_box(3) = max(tighest_box(3), p(3));
tighest_box(4) = max(tighest_box(4), p(4));
%% move all axes to left-bottom
Hi, I found two problems with this function:
#1 : Is doesn't works if the figure is a rectangle.
#2: The colorbar still get forced to be inside the figure.
I tried to use your function with this figure
figure,[C,h] = contourf(peaks(20),10);
axis equal, axis([1 12 1 6])
Thanks a lot.
Thanks I'm using your script to save my figures :)
Andy, it's probably because you are re-using the same figure window for multiple saves, are you? I would suggest to create a new figure for each of your plots. Your code will run slower but the problem will probably be fixed. Thanks.
This is very helpful.
!But when I run the script as batch job. The generated graph is too tight, some caption is invisible.
Nishant, you are right, I updated the file. Thanks!
I think You should chnge lines 13-18 to this
hfig = gcf;
outfilename = h;
hfig = h;
or else passing a handle will throw an error
Thanks for the heads up Susanna. It's fixed now. It seems that legend position is automatically changed when the main axes position is changed. So, I fixed the script accordingly.
Very useful function - it reduces the white margins around printed/saved figures, but it has problems with the legend.
In my case, the legend was originally located 'southoutside' with horizontal orientation, but the saveTightFigure function pushed it up so that it ended up overlapping with the x-axis.
I'm not sure what is causing this. Would be great if it could be fixed.
Following Geoff's suggestion, I managed to fix it (for my own figure), by changing the second 'for i=1:length(hax)' to for i=length(hax):-1:2'
It works correctly for this example:
Can you provide a simple example to reproduce the problem?
Very useful function, thanks! Unfortunately, even with the all-axes update, I've been finding that my colorbar (standard, vertical on the right) is being pushed too far left such that it's on top of the figure. Not sure why, but changing the second 'for i=1:length(hax)' to 'for i=length(hax):-1:1' seems to fix this, at least for the specific figure I'm working with. Any idea why?
Ubaldo, if the xlabel and title are overlapping in the actual figure displayed, then it will be reflected in the output. So, you have to make sure your figure is big enough for prevent those overlaps.
Ubaldo, I updated the file to handle subplots. Please check if it works for you.
I have found an update that also handle suplots here:
However, when you use subplots the x-axis names overlap with the title of the next subplot. Is there any further update planned of this very nice and useful function??
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!