Add margin to pdf files using exportgrahics function
15 views (last 30 days)
Show older comments
Hi team
I am using exportgrahics function within my loop to append figures as pdf files and merge them in one pdf file as a final outcome. All good except there are absolutely no margin at all. I want to add atleast half an inch margin on all sides (top bottom left right). I have tried different things but can't add margin to my final pdf file. Can someone please help me? I have pasted part of my codes here. You can just generate dummy plots in loop and please use exportgraphics to merge them in pne file as pdf.
If exportgraphics is not suitable function then please let me know alternative codes. I have also attached my output pdf file. Thank you.
% FIG 1
figure;
plot(Date, Close, 'b-', 'LineWidth', 1, 'DisplayName', company);
grid on;
xlabel('Year');
ylabel(company);
xlim([min(Date), max(Date) + 200])
ylim([min(Close), max(Close) * 1.1])
ytickformat('%,.0f');
hold on
scatter(Px, Py, 50, 'green', 'filled', 'DisplayName', 'Positions');
hold off
lgd = legend;
lgd.NumColumns = 3;
yyaxis right; % Secondary axis
plot(Date_N, Close_N, 'k', 'LineWidth', 0.25, 'DisplayName', 'NIFTY-50');
ylabel('NIFTY-50');
xlim([min(Date_N), max(Date_N) + 200])
ylim([min(Close_N), max(Close_N) * 1.1])
set(lgd, 'Location', 'northwest');
title(company, newline);
ax = gca;
ax.YAxis(2).Exponent = 0;
ax.YAxis(1).Color = 'k';
ax.YAxis(2).Color = 'k';
ytickformat('%,.0f');
set(gcf, 'PaperOrientation', 'landscape', 'Visible', 'off', ...
'Position', [100, 100, 1100, 550]);
exportgraphics(gcf, output_pdf, 'Append', true);
0 Comments
Accepted Answer
Yash
on 16 Feb 2025
Edited: Yash
on 17 Feb 2025
There are two ways to add padding to figures saved by exportgraphics:
1. Use Padding parameter. Please note that this parameter is supported in MATLAB Online only (since R2024a)
exportgraphics(gcf, 'fileName.pdf', 'Padding', 100) % Padding can be 'tight', 'figure' or a positive number
Refer to documentation: https://www.mathworks.com/help/matlab/ref/exportgraphics.html#mw_c1367a50-523c-47e6-a207-fcb49c39db87
2. You can force exportgraphics to add padding by drawing a white rectangle around it using the function annotation.
bar(1:5)
title("My Bar Chart")
annotation('rectangle',[0 0 1 1],'Color','w');
exportgraphics(gcf, 'fileName.pdf')
Refer to this MATLAB Answer post for more discussion on this topic: https://www.mathworks.com/matlabcentral/answers/861570-exportgraphics-adding-a-margin
More Answers (0)
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!