How do I stop exportgraphics from adding a border

16 views (last 30 days)
Dean
Dean on 14 Nov 2025 at 18:12
Commented: dpb about 7 hours ago
Hi Everyone
I'm using Matlab 2024b and have found an oddity.
If I load an image and then export it again exportgraphics seem to add a white border
In its simplest form...
fig = figure();
im = imread('peppers.png');
imshow(im)
exportgraphics(gca,'exportplot.jpg');
If you compare peppers.png with the exported version then you can see a pixel wide border has been added
I've tried
set(gca, 'color', 'none');
axis off
exportgraphics(gca,'plot2.jpg','BackgroundColor','none')
but none of these seem to make any difference. What's annoying is that I'm also using exportgraphics to generate animated gifs but the white margin appears around those too. This really shows up on the black background that I'm using.
Any suggestions on how to eliminate this?

Answers (1)

dpb
dpb about 20 hours ago
Edited: dpb about 16 hours ago
That would be expected behavior...
Background color, specified as 'current', 'none', an RGB triplet, a hexadecimal color code, or a color name. The background color controls the color of the margin that surrounds the axes or chart.
  • A value of 'current' sets the background color to the parent container's color.
  • A value of 'none' sets the background color to transparent or white, depending on the file format and the value of ContentType:
  • Transparent — For files with ContentType='vector'
  • White — For image files, or when ContentType='image'
  • When ContentType='auto', MATLAB sets the background color according to the heuristic it uses to determine the type content to save.
  • Alternatively, specify a custom color or a named color.
Try
exportgraphics(gca,'plot2.jpg','BackgroundColor','auto')
and see if it's smart enough to match background closely enough to not be objectionable. If not you may have to set a specific RGB color.
It doesn't appear there's a way to not have a bounding box of some color.
  2 Comments
Dean
Dean about 1 hour ago
Thanks for your reply. I've tried these options.
exportgraphics(gca,'plot2.jpg','BackgroundColor','auto')
still gives a white border
but
exportgraphics(gca,'plot2.jpg','BackgroundColor','k')
replaces the white border with black. Not ideal but I can live with this and it at least allows the image to blend better into the background. So thanks for the help.
On a similar (but related note). Is there a similar handle or option for tabs in App designer? I'd ideally like the tabs to look invisible on a black background but these too seem to have an annoying white border than I cannot seem to eliminate. The tab background setting does not affect the border and the parent TabGroup doesn't appear to have any color settings.
The same is true for panels in panels. It would be nice to use these panels for layout configuration but not have the border showing.
As you can see below the white border stands out when you don's want it and doesn't seem to have any obvious settings.
dpb
dpb 22 minutes ago
I would suggest you submit the request for a 'Box' named parameter analogous to that for the axes properties to Mathworks as an official enhancement request at <Product Support Page>.
As for the tabs; there has been at least one other thread of a similar nature with the same conclusion; at the moment there is no control available to achieve the desired visual effect and that desires/requirements be submitted as enhancement request. Such user input does at least influence future developments but without that input there will not be any real impetus for changes.

Sign in to comment.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!