Is it possible to add a margin to figures saved using the exportgraphics function?
It crops a tad too tightly for my needs.

 채택된 답변

David Hasin
David Hasin 2024년 10월 7일
편집: David Hasin 2024년 10월 7일

0 개 추천

Hey there, you can use 'padding' in the exportgraphics option, but it is available only in matlab online 2024a. For example:
exportgraphics(ax,"figurepadding.png","Padding","figure")
or
exportgraphics(ax,"figurepadding.png","Padding",20)
See documentation:

추가 답변 (4개)

Paul Wintz
Paul Wintz 2021년 9월 15일
편집: Paul Wintz 2021년 11월 18일

6 개 추천

You can force exportgraphics to export the entire figure by drawing an white rectange around it with the command
annotation('rectangle',[0 0 1 1],'Color','w');
To adjust the margins, change the dimensions [0 0 1 1].
As noted by @David Walwark in the comments, the plot will not be interactive while it is annotated. To restore interactivity, delete the annotation after calling exportgraphics.
a = annotation('rectangle',[0 0 1 1],'Color','w');
% -> Save figure here <-
delete(a)

댓글 수: 4

Fatemeh Sadeghihassanabadi
Fatemeh Sadeghihassanabadi 2021년 10월 11일
Thanks @Paul Wintz, I had the same problem, this is the best solution for it.
David Walwark
David Walwark 2021년 11월 17일
Excellent, this really helped. If one wants the figure to remain interactive then it is best to delete the handle to that annotation after exportgraphics is called.
Bill Tubbs
Bill Tubbs 2023년 3월 23일
편집: Bill Tubbs 2023년 3월 23일
This didn't work for me. Presumably when doing 2 subplots you need to apply this annotation to each subplot?
I tried adjusting the margins to [-0.05 -0.05 1.05 1.05] and it raises:
Error using annotation (line 116)
Position values must be between 0 and 1.
Alexander Moody
Alexander Moody 2023년 8월 28일
Had the same issue... this worked for me, but I had to change the color of the annotation rectangle to something other than white for it to work.

댓글을 달려면 로그인하십시오.

Kiran Felix Robert
Kiran Felix Robert 2021년 6월 24일

1 개 추천

Hi Dormant,
If you are trying to export multiple sub-images/plots onto a single figure, you can explore the tiledlayout to introduce spacing using the 'TileSpacing' Name-Value pair.

댓글 수: 2

Paul Wintz
Paul Wintz 2021년 9월 15일
This doesn't answer the question. How do we adjust the margins on the outside of a figure saved with exportgraphics?
Bill Tubbs
Bill Tubbs 2023년 3월 23일
This worked for me. When I did my 2-plot figure using tiledlayout instead of subplots the outer margin was significantly wider.

댓글을 달려면 로그인하십시오.

dormant
dormant 2024년 10월 8일

0 개 추천

Many thanks to you all. I gave up on this approach as I needed to specify sizes exactly. So there's a new question here: https://www.mathworks.com/matlabcentral/answers/2158290-specify-plot-box-position-for-tiled-layout?s_tid=srchtitle
Andreas Sprenger
Andreas Sprenger 2025년 1월 28일

0 개 추천

I use the plotedit function to refrain cropping of plots (particularly with multiple subplots.
hfig = figure('WindowState', 'maximized');
plotedit(hfig);
% subsequent plotting commands
You get a tiny blue line at the outer border of the page but the cropping is gone :-)

카테고리

도움말 센터File Exchange에서 Printing and Saving에 대해 자세히 알아보기

제품

릴리스

R2020b

태그

질문:

2021년 6월 21일

답변:

2025년 1월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by