There are a lot of options of how to save a figure in Matlab. If you do not use a Save As dialog box, you have two functions to choose from: saveas and print.
'Position'
defines location and size of the drawable area, specified as a vector of the form [left bottom width height]. This area excludes the figure borders, title bar etc. Right now you are basically getting the size and location of your first figure as it appears on screen and save based on these dimensions.
When saving your figures this way, the dimensions will correspond to whatever internally was defined in Matlab or you yourself redefined using 'Position'
property. But you don't always want/need the size of the saved figure and the size of the figure as it appears on screen to be the same. And you also have to take care of the position of your figures, which is in your case you retrieved using a set
function, I'll skip it in my example.
gcf=figure;
figure_width_to_save = 12.5; %cm
figure_height_to_save= 10; %cm
location_x=2; %cm
location_y=2; %cm
gcf.Units = 'centimeters';
gcf.Position = [location_x location_y figure_width_to_save figure_height_to_save];
saveas(gcf,[savefigures_path,savefigure_name,'_saveas.tiff'],'tiffn');
print(gcf, '-dtiffn', [savefigures_path,savefigure_name,'_print.tiff'], '-r300');
But it's better to have a separate control over the settings used for saving a figure. For that you have to define 'PaperPosition'
property. 'PaperPosition'
defines figure size and location on page when saving, specified as a four-element vector of the form [left bottom width height], but actually with 'PaperPosition'
property you don't need to think about the location of your figure as much as you would with the 'Position'
property.
Now about the saving itself, you didn't mention which approach you use though.
The saveas function uses a resolution of 150 DPI and uses the 'PaperPosition'
and 'PaperPositionMode'
properties of the figure to determine the size of the image. If you want to print or save figures that are the same size as the figure on the screen, ensure that the 'PaperPositionMode'
property of the figure is set to 'auto'
, but I prefer to have control over these properties myself.
If you save your figure in Matlab with saveas, then as an example you need to specify this:
gcf.PaperPositionMode = 'manual';
gcf.PaperUnits = 'centimeters';
gcf.PaperPosition = [0 0 figure_width_to_save figure_height_to_save];
saveas(gcf,[savefigures_path,savefigure_name,'.tiff'],'tiffn');
Function print additionally allows you to have control over the saved resolution of the figure. For example, a flag '-r300' sets the output resolution to 300 dots per inch. To specify screen resolution, use '-r0'.
print([savefigures_path,savefigure_name,'.tiff'],'-dtiffn','-r300')
Check out Matlab's examples about saving figures at specific size and resolution