Automate saving graphs in high resolution .pdf

2 次查看(过去 30 天)
Hi everyone!
I'm trying to create a code to export my graphs in high resolution .pdf. I also need that the image has little white spce in the borders.
The closest I could get to do this was with this code:
fig = figure('PaperOrientation','landscape');
stairs(t, h, 'LineWidth', lineWidth);
grid;
hold on;
stairs(t, hPos, 'r--', 'LineWidth', lineWidth);
stairs(t, hNeg, 'r--', 'LineWidth', lineWidth);
xlim([0 finalTime/4]);
legend({'Resposta ao impulso'});
xlabel('Tempo (s)');
ylabel('Amplitude (V)');
set(fig, 'PaperOrientation', 'landscape');
set(fig,'Units','Inches');
pos = get(fig,'Position');
set(fig,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
%saveas(fig, 'Imagens/sinaisPWM.pdf');
print(fig, 'Imagens/sinaisPWM.pdf', '-dpdf', '-bestfit');
From witch I got this image:
You could help me reducing the borders of this image and improving the code above? Is better to use 'saveas' or 'print'?
Thank you!

回答(1 个)

Neelanshu
Neelanshu 2024-2-5
Hi Clecio,
I understand from your query that you are interested to reduce the borders of the exported image.
You can use "exportgraphics" instead, when you want to save a plot and include it in documents or presentations. It crops the output tightly around the axes, legends, and colorbars. Here is the modified code for the same:
lineWidth = 2; % Example line width, replace with your actual value
finalTime = 100; % Example final time, replace with your actual value
t = 0:0.1:finalTime; % Example time vector, replace with your actual data
h = sin(t); % Example data, replace with your actual data
hPos = sin(t) + 0.5; % Example data, replace with your actual data
hNeg = sin(t) - 0.5; % Example data, replace with your actual data
% Create the figure
fig = figure;
stairs(t, h, 'LineWidth', lineWidth);
grid on;
hold on;
stairs(t, hPos, 'r--', 'LineWidth', lineWidth);
stairs(t, hNeg, 'r--', 'LineWidth', lineWidth);
xlim([0 finalTime/4]);
legend({'Resposta ao impulso'}, 'Location', 'best');
xlabel('Tempo (s)');
ylabel('Amplitude (V)');
% Save the figure as a PDF with minimal white space using exportgraphics
exportgraphics(fig, 'sinaisPWM.pdf', 'Resolution', '300'); % setting the resolution to 300 dpi
You may refer the following documentation to learn more about the different ways to export graphics from figures :
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by