How do I add a background image to my GUI or figure window?

240 views (last 30 days)
The MATLAB demo window has a background image in it, and I would like to put a background image in my figure.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Feb 2013
The following code will let you create a background image in your figure. To do this, you need to create an axes object that spans the size of the figure, and then place the image inside this axes.
% This creates the 'background' axes
ha = axes('units','normalized', ...
'position',[0 0 1 1]);
% Move the background axes to the bottom
uistack(ha,'bottom');
% Load in a background image and display it using the correct colors
% The image used below, is in the Image Processing Toolbox. If you do not have %access to this toolbox, you can use another image file instead.
I=imread('eight.tif');
hi = imagesc(I)
colormap gray
% Turn the handlevisibility off so that we don't inadvertently plot into the axes again
% Also, make the axes invisible
set(ha,'handlevisibility','off', ...
'visible','off')
% Now we can use the figure, as required.
% For example, we can put a plot in an axes
axes('position',[0.3,0.35,0.4,0.4])
plot(rand(10))
In a GUI .m file created by GUIDE, the easiest place to put this code is in the "CreateFcn" for the figure window. This will cause it to be executed when the GUI is created.
One problem which may arise is due to the handle visibility of the GUI. If a new figure window appears when you execute this code in your GUI, please change the HandleVisibility property of the GUI figure as described in the solution attached at the bottom of this page.
If you want to change the transparency of this background image, you can set the "AlphaData" property of the image. An example of how to do this is shown in the example code below:
set(hi,'alphadata',.5)
Please note that this will not change the transparency of the GUI or the figure window.
  8 Comments
YU RONG
YU RONG on 28 Jan 2021
a lot thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Sign in to comment.

More Answers (1)

Ka Mirul
Ka Mirul on 17 Oct 2017
Hallo, I have seen a tutorial about "Set an Image as GUI Background" at https://www.youtube.com/watch?v=-qjwVjrnrWU and it works. Hope it will help you.

Categories

Community Treasure Hunt

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

Start Hunting!