
Automate saving graphs in high resolution .pdf
3 ビュー (過去 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!
0 件のコメント
回答 (1 件)
nick
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Printing and Saving についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!