Saving figure as a image

24 views (last 30 days)
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:
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.
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],...
% Create axes
axes2 = axes('Parent',figure1,...
'XColor',[56/255 87/255 35/255],...
'YColor',[56/255 87/255 35/255],...
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,...
'XColor',[35/255 65/255 115/255],...
'YColor',[35/255 65/255 115/255],...
% ..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);
Thanks for all your help!!

Sign in to comment.

More Answers (1)

David Hill
David Hill on 1 Apr 2021
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.


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


Community Treasure Hunt

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

Start Hunting!