How to increase margin when export graphics-Illegal option 'padding' given

43 次查看(过去 30 天)
Hi there,
When exporting graphs, the margins were too narrow. Thus, i set the padding following the instruction https://www.mathworks.com/help/matlab/ref/exportgraphics.html
However, error message shown even when I simply copied and paste the example from the help center.
bar(1:5)
title("My Bar Chart")
ax = gca;
exportgraphics(ax,"100pixelpadding.png","Padding",100)
Error using exportgraphics
Illegal option 'padding' given.

回答(2 个)

Hitesh
Hitesh 2025-2-7
编辑:Hitesh 2025-2-7
Hi Huei-Ting,
I too encountered the similar issue and I am assuming that you are running this piece of code on MATLAB Desktop. In the MATLAB documentation, they have mentioned that "padding" property is Supported in MATLAB Online only (since R2024a).
Kindly refer to the MATLAB Documentation:
You need to use MATLAB Online to use "padding" property since it is not supported in Desktop version. It works in MATLAB online version. It also works in MATLAB R2025 prerelease version. Most probably they will add it in the Desktop version as well.

Vedant Shah
Vedant Shah 2025-2-7
The “Padding” attribute of the “exportgraphics” function is supported in MATLAB Online R2024a and onwards. It is not supported in MATLAB Desktop version, as mentioned in the following documentation:
However, if you are using the desktop version, there is another way to add padding to the graph. The code for this is shown below:
figure;
bar(1:5);
title("My Bar Chart");
% Adjust figure size
set(gcf, 'Position', [100, 100, 600, 400]); % [left, bottom, width, height]
saveas(gcf, 'mybarchart.png');
This code provides the desired output:
For more information about the “set” and “saveas” functions, you can refer to the following documentation:
  2 个评论
Huei-Ting
Huei-Ting 2025-2-17
编辑:Walter Roberson 2025-2-17
I tried
set(gcf, 'Position', [100, 100, 600, 400]); % [left, bottom, width, height]
or even
set(gcf, 'Position', [200, 200, 1200, 800]);
Unfortunately, it is still not working. Perhaps I need to keep testing the variables to find out how it will work after I export the figure. They look good on the matlab figure but still leaving no margin after I exported the .tif file.
Tina
Alois Gmur
Alois Gmur 2025-5-30
Hello,
I am also using the 2024b Version where neither Padding, Width, Height nor Units is a legal command.
Could you find a solution to this problem in the meantime?
Help would be greatly appreciated!
Kind Regards

请先登录,再进行评论。

类别

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