Matlab GUI Tutorial - Custom Background Images and Custom Buttons
28 Nov 2007 Quan Quach 14 comments 16222 views
Changing the Background Image
In this part of the tutorial, we will learn how to add this image to the background of a GUI.

-
First, download the sample GUI provided here. Unzip the files and place them wherever you please. The zip file includes 4 files: customImage.fig, customImage.m, sunset-beach.jpg, and smiley.jpg.
-
Now, type
guideat the command prompt.
-
Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

-
Here is what the GUI should look like when you open it:

If you look closely, you’ll notice that it’s the same GUI used in the Slider Tutorial.
-
You might have noticed that the background image and the GUI size are not exactly the same size. If you try to put an image that is too large or too small over the GUI, it won’t quite look right. So we should make sure that GUI and the picture are the correct size. In this instance, I’m going to change the GUI size to conform to the background image. The background image has dimension of 448 by 336 pixels, so lets make the GUI figure the same size. Double click on the GUI grid to bring up the property inspector and change the Units property to
pixels.
-
Next, go to the Position Property and click on the
+. Change the Width field to448and the Height field to336as shown below.
-
Next, we need to add an axes component onto the GUI figure. This is where the background image will be place. Make sure the axes component covers the entire GUI figure, and change the Tag property to
axes1. Remember to save your GUI. Your GUI figure should now look like this:
-
Click on the
icon on the GUI figure to bring up the accompanying .m file. -
Now, add the following code to customImage_OpeningFcn.
%load the background image into Matlab %if image is not in the same directory as the GUI files, you must use the %full path name of the iamge file backgroundImage = importdata('sunset-beach.jpg'); %select the axes axes(handles.axes1); %place image onto the axes image(backgroundImage); %remove the axis tick marks axis off
-
Now, save your .m file and run the GUI. You should see the following GUI appear

14 Responses to “Matlab GUI Tutorial - Custom Background Images and Custom Buttons”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>

Using this approach you can make your GUI like a Mac theme with glossy buttons! I have received a lot of comments when doing this. So I encourage you to do the same.
thanks for the encouragement, turtie! hope the coming holiday season treats you well
I’ve added an axes control on the figure, but I can’t seem to find the function for it in the m-file. I just want to load up a simple image on the figure when the program runs. Is there another way to do this?
decypher,
I don’t know of another way to do this.
The function you are looking for is the “opening function”. This function runs when the GUI is executed. Within this function, you set the code to display the image. Please try it again and come back if you are not able to get it to work.
Quan
Hi,
This 3 page tutorial was very useful. When I include a background picture in the gui, the panels are disappearing. But I just want my panel border to be visible in the gui. How do I do this?
Bbye,
Priyanka.
hi.
interesting post….
however if 1 call the gui again.. the background image is not loaded….
any idea how to handle this prob…
thnks
regards,
nishna
Hey guys thanks for all the wonderful tutorials youve created, really has helped with my final year project, ive used your tutorials to create several guis which i can launch from one main menu gui for various aspects. But i was wondering how to i set matlab, or gui properties so that everytime i press the button on the main menu to launch the gui, it runs in the exsisting figure windows instead of opening a new one each time?
Thank you
hi
thanks a ton for ur tutorials its helped me lot. But i have a problem.. plz help me out. I am not able to display 2 dimension images in exact way it is in GUI axes of matlab. the colors and intensity varies a lot. Can you please let me know how to do it..
thank you
Hello Ramya,
What type of image are you trying to put in your background?
Quan
no i am not trying to put in background. but i am trying to dispay an image which is not RGB image. the image type with two planes i want to display like suppose an image f i want to display f(:,:,1) in the axis. Can you please help me out?? is this possible?
Hi all,
Any idea how to put in a picture in a gui with Matlab being on the Linux platform?
I have a GUI design but would like to add a picture.
Cheers
Froggy007
THANX ALOT!!
Really nice post!
Thanks for the tutorial.
Like many noted before me: your explanations are concise, to the point, clear and complete. Thanks.