MATLAB: How to add a background image to the GUI or figure window

backgroundfigureguiimageMATLABpicture

The MATLAB demo window has a background image in it, and I would like to put a background image in my figure.

Best Answer

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.