Saving figure as a image

25 views (last 30 days)
meghannmarie
meghannmarie on 1 Apr 2021
Commented: meghannmarie on 16 Apr 2021
I am saving my output figures as images. But when I run my matlab script from different computers, the figures look different. As in the labels and legends are different sizes, etc. I want the image outputs to be exactly the same no matter what computer I run it on, I do not want to have computer specific scripts.
I am not a graphics person, so I do not know what causes this. Whether it is the screen resolution, renderer, file type, options or what? I do not know where to start.
The code I am using to save figure is:
output_file = 'example_image.png'
saveas(figure1,fullfile(folderName, output_file));
which outputs the following image:
Help!!
  9 Comments
Adam Danz
Adam Danz on 13 Apr 2021
From the figure menu, when I select File > SaveAs > PDF the results look quite similar.
meghannmarie
meghannmarie on 13 Apr 2021
Edited: meghannmarie on 13 Apr 2021
What I am trying to do is get graphics that look exactly the same no matter what computer they are run on. I am running the same scripts (and data) on our high performance computer and my local laptop and getting graphics that look different. I do not want to have to tweak graphics depending on what computer they are run on (it can be 100s of graphics). I need them to be the same because then they are emdedded into another document.
I do not need to save as pdf, that was just the other persons suggestions, I would prefer an image file.
If you look at .png image outputs from each computer, they look different. The axes are labeled differently.
I am thinking it is something to do with the size of figure window/fonts and how it is choosing what labels to show or not to show that is making it different...

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 13 Apr 2021
Comparing the two png files in one of your previous comments shows an upper y axis in one figure that doesn't exist in the second figure. This is more than a version difference. The only why that would be possible is if the two systems are using different versions of the code or if the system using the older Matlab release is quitting early due to an error.
There is also a difference in fontsize. That can be fixed by explicitly setting the fontsizes in your code. The difference in figure size can be controlled by explicitly setting figure size.
  10 Comments
Adam Danz
Adam Danz on 13 Apr 2021
Regarding the inconsistent figure sizes, there are many areas to troubleshoot and I've found that exportgraphics does some unexpected things with figure size. For example in this example from the documentation, the printed fig should be "approximately 3 inches" but it's much larger when i try it (r2021a).
The export_fig function on the file exchange will likely work for you:
meghannmarie
meghannmarie on 16 Apr 2021
The export_fig fuunction still did not work. I ended up putting in a tech support ticket. There were a few differences in the computers that were causing the differences. One computer was windows and other computer was linux where I was using command prompt to run scripts. On the different computers the renderers were different, the screen resolutions were different, and they had different fonts. Once I compensated for all this (plus the other things you helped me with), then the graphics turned out very similar. Only differences left are slight differences in fonts and file size which both are just because its a different OS.
I attached the final graphics and here is my final code:
% Set Renderer
opengl software
set(groot, 'DefaultFigureRenderer', 'painters');
% Create figure
figure1 = figure('Position',[100,100,550,450],...
'PaperPositionMode','auto',...
'InvertHardcopy','on',...
'Visible','off');
% Create axes
axes2 = axes('Parent',figure1,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'XColor',[56/255 87/255 35/255],...
'YColor',[56/255 87/255 35/255],...
'Color','None',...
'FontSizeMode','manual',...
'FontName','Arial',...
'FontSize',10);
hold(axes2,'on');
axis(axes2,'ij');
title(axes2,{[parameter ' vs. Depth' ],month_long{n}})
% ..more code here
% Create plot
ax2_pos = axes2.Position; % position of first axes
axes1 = axes('Position',ax2_pos,...
'XAxisLocation','bottom',...
'YAxisLocation','left',...
'XColor',[35/255 65/255 115/255],...
'YColor',[35/255 65/255 115/255],...
'Color','none',...
'FontSizeMode','manual',...
'FontName','Arial',...
'FontSize',10);
linkprop([axes1,axes2],{'Position'});
% ..more code here
% output resolution and format
outputDPI = '-r900';
outputFmt = '-dpng';
% control computer (hpc) screen pixel's per inch
dispPPI = 72;
% current computer's screen pixel's per inch
ppi = get(0, 'screenpixelsperinch');
% compensate for different DPI
scale = ppi / dispPPI;
figure1.Position(3:4) = figure1.Position(3:4)*scale;
name = fullfile(folderName,output_file);
print(figure1, name, outputFmt, outputDPI);
close(figure1);
Thanks for all your help!!

Sign in to comment.

More Answers (1)

David Hill
David Hill on 1 Apr 2021
  2 Comments
meghannmarie
meghannmarie on 1 Apr 2021
It seems like this function is just outputting file to a pdf, which I need a graphic file. I will try it when I have access to my second computer, but do you know what in theis code makes the graphics the same?
Adam Danz
Adam Danz on 13 Apr 2021
meghannmarie's answer moved here as a comment
-----------------------------------------------------------------------------
The figure2pdf function does not work. The pictures still save differently on different machines.
Look at my attachments, one is saved on my laptop and one is saved on our hpc. They are different, but same exact script. I think it is something to do with resolution of screen, but how do I get around it?

Sign in to comment.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!