Add margin to pdf files using exportgrahics function

조회 수: 10 (최근 30일)
Amit Patel
Amit Patel 2025년 2월 14일
편집: Yash 2025년 2월 17일
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);

채택된 답변

Yash
Yash 2025년 2월 16일
편집: Yash 2025년 2월 17일
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
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by