I am producing images of matrices with imagesc() and want to save them exactly as they are on the screen. Each pixel in my data is assigned a certain number which corresponds to a color from the colormap. In my MWE the data is random so it is easier to provide.
Because I want to compare two consecutive timesteps I am using a tiled layout to have the two timesteps next to each other in one picture. I create the tiled layout, call imagesc() on the two tiles and want to save the whole thing.
Now the problem: I do not find a way to save that actually preserves each pixel. No matter the resolution (I already tried 2000dpi) or the method, there is always a difference between the produced png and the data that is shown on screen. I have verified that the display from imagesc in the figure corresponds to the data matrix, so the difference comes up in the saving process.
I have tried multiple functions for saving (print, save, saveas, exportgraphics, imwrite), but of course maybe I have missed some critical option when using them.
Any ideas how to preserve each pixel exactly?
PS:
- I do not require the outut to be pictures (png, jpg, ...), but would prefer it.
- I do not need the plot to be shown on screen, I would be fine if it is saved directly.
MWE:
data1 = randi(3, dim1, dim2);
colors1 = [[1 0 0]; [0 1 0]; [0 0 1]];
data2 = randi(4, dim1, dim2);
colors2 = [[1 0 0]; [0 1 0]; [0 0 1]; [1 1 0]];
t.TileSpacing = 'compact';
title('First small plot')
title('Second small plot')
print(fig, 'saved_with_print', '-dpng', '-r300');
print(fig, 'saved_with_print_and_painters', '-dpdf', '-painters', '-fillpage')