How to convert current figure to matrix 1:1?

2 views (last 30 days)
giannit
giannit on 28 Aug 2020
Edited: giannit on 28 Aug 2020
The following code firstly saves the figure to an image, then converts the same figure into a matrix which however has not the same resolution (in pixels) of the image, that is the matrix has a different number of rows and columns, why does this happen?
figure('visible','off')
fplot(@(x) sin(x),[0 2*pi])
saveas(gcf, 'test_saveas.png') % 656x875
im = frame2im(getframe(gcf));
imwrite(im, 'test_frame2in.png') % 420x560
[size(im) ; size(imread('test_saveas.png'))]
ans =
420 560 3
656 875 3
Is it possibile to convert the figure into a matrix preserving the resolution?

Answers (1)

Walter Roberson
Walter Roberson on 28 Aug 2020
saveas() uses print()
print() uses the figure PaperSize and PaperPosition, and a default resolution that appears to be 100 pixels per inch. It does not just capture the screen: it internally redraws the figure at the resolution implied by PaperSize, and saves that.
getframe() on the other hand, uses whatever the screen resolution and figure size is.
"Is it possibile to convert the figure into a matrix preserving the resolution?"
Preserving which resolution? The figure with the toolbar cropped out? The axes OuterPosition ?
If you are using a high resolution screen and MATLAB has silently rescaled for you, which resolution do you want to use?
  1 Comment
giannit
giannit on 28 Aug 2020
Edited: giannit on 28 Aug 2020
Thank you Walter for support! I'm using a HD ready monitor (1366x720), I would like to have a matrix whose size is the same as the image saved using the saveas command, that is a matrix with 656 rows and 875 columns.
The images generated with saveas and with frame2im+imwrite are identical except for the pixels resolution https://imgur.com/a/3Q8nf7Y
The problem has not to do with axes I think, since the same happens if we just plot annotations or even nothing, for example
figure('visible','off')
saveas(gcf, 'test_saveas.png') % 656x875
im = frame2im(getframe(gcf));
imwrite(im, 'test_frame2in.png') % 420x560

Sign in to comment.

Categories

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

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!