How to save a plot as jpeg or pdf with a given aspect ratio and resolution?
Show older comments
I need to save a plot generated by my matlab code as a jpeg or pdf file. The simple 'saveas' command works, but the resolution is poor, the aspect ratio of the generated jpeg is not the one I want (it seems to generate pictures only in a given aspect ratio) because of which the legend table and/or the axes get cut in the resulting picture file. Please help me with the codes that will save my plot as a jpeg or pdf with the resolution and aspect ratio specified in the program.
Answers (1)
Raghava S N
on 20 Nov 2024
To specify the aspect ratio of a figure with a plot in MATLAB, you can use the “set” function.
width = 6; height = 8;
set(gcf, 'Units', 'inches', 'Position', [0, 0, width, height]); %set the width and height for the current figure
For more information, refer to this link - https://www.mathworks.com/help/matlab/ref/set.html#mw_d9b1d234-44ce-47ae-84b2-0eb9a809659f:~:text=(for%20example%2C%20Figure%20Properties%2C%20Axes%20Properties%2C%20Line%20Properties%2C%20and%20Text%20Properties).
To save a plot with a specified resolution and aspect ratio in MATLAB, you can use the “print” function, which provides more control over the output quality and dimensions. The resolution can be passed as a parameter to the “print” function-
resolution = 300; % DPI
print(gcf, '-djpeg', ['-r', num2str(resolution)]);%save the current figure as a .jpeg image file
For more information about the “print” function and its parameters, refer to this link- https://www.mathworks.com/help/matlab/ref/print.html#:~:text=resolution%20%E2%80%94%20Resolution.
Hope this helps!
Categories
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!