Automate saving graphs in high resolution .pdf
2 views (last 30 days)
Show older comments
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 Comments
Answers (1)
nick
on 5 Feb 2024
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 Comments
See Also
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!