How to save a plot as jpeg or pdf with a given aspect ratio and resolution?

6 ビュー (過去 30 日間)
TP Das
TP Das 2015 年 1 月 26 日
回答済み: Raghava S N 2024 年 11 月 20 日 9:40
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.

回答 (1 件)

Raghava S N
Raghava S N 2024 年 11 月 20 日 9:40
Hello @TP Das,
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
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!

Community Treasure Hunt

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

Start Hunting!

Translated by