Matlab GUI Tutorial - Saving Plots within a GUI
08 Nov 2007 Quan Quach 21 comments 5,325 views
Introduction
This tutorial will show you how to save an image, graph, or plot on a GUI’s axes to a graphics file. Being able to save plots within a GUI is a helpful feature for the average end user; unfortunately, there is no built in function/command that will allow you to do this. The function that is supplied in the next section will allow you to save any image, graph, or plot exactly as seen on your GUI’s Axes component.
This tutorial is written for those with some experience creating a Matlab GUI. If you’re new to creating GUIs in Matlab, you should visit this tutorial first. Basic knowledge of Matlab and an understanding on how data is shared among callbacks is highly recommended. Matlab version 2007a is used in writing this tutorial. Both earlier versions and new versions should be compatible as well (as long as it isan’t too outdated). Let’s get started!
The Function Code
The function shown below has two input arguments, axesObject (required) and legendObject (optional).
The axesObject is simply the tag of the Axes component on the GUI. The legendObject is the object created when you use the legend command in Matlab.
A quick rundown of what this function does:
- opens up a new figure
- copies the contents of the axes object onto the new figure and adjusts the new figure
- if a legend object is included in the function input, copies the contents of the legend object onto the new figure adjusts the new figure
- saves the figure to either .emf, .bmp, or .fig file. (.jpg and .png were not used because Matlab is unable to display it properly)
- closes the new figure
Confused? Don’t worry, as there will be some sample code to clear things up in the next section.
%created by: Quan Quach %date: 11/8/07 %function to save plots within a GUI function savePlotWithinGUI(axesObject, legendObject) %this function takes in two arguments %axesObject is the axes object that will be saved (required input) %legendObject is the legend object that will be saved (optional input) %stores savepath for the phase plot [filename, pathname] = uiputfile({ '*.emf','Enhanced Meta File (*.emf)';... '*.bmp','Bitmap (*.bmp)'; '*.fig','Figure (*.fig)'}, ... 'Save picture as','default'); %if user cancels save command, nothing happens if isequal(filename,0) || isequal(pathname,0) return end %create a new figure newFig = figure; %get the units and position of the axes object axes_units = get(axesObject,'Units'); axes_pos = get(axesObject,'Position'); %copies axesObject onto new figure axesObject2 = copyobj(axesObject,newFig); %realign the axes object on the new figure set(axesObject2,'Units',axes_units); set(axesObject2,'Position',[15 5 axes_pos(3) axes_pos(4)]); %if a legendObject was passed to this function . . . if (exist('legendObject')) %get the units and position of the legend object legend_units = get(legendObject,'Units'); legend_pos = get(legendObject,'Position'); %copies the legend onto the the new figure legendObject2 = copyobj(legendObject,newFig); %realign the legend object on the new figure set(legendObject2,'Units',legend_units); set(legendObject2,'Position',[15-axes_pos(1)+legend_pos(1) 5-axes_pos(2)+legend_pos(2) legend_pos(3) legend_pos(4)] ); end %adjusts the new figure accordingly set(newFig,'Units',axes_units); set(newFig,'Position',[15 5 axes_pos(3)+30 axes_pos(4)+10]); %saves the plot saveas(newFig,fullfile(pathname, filename)) %closes the figure close(newFig)
How to Apply the Function
I have supplied a sample GUI here that you can download to get a better idea of how it all works. Unzip the files (there should be three files) and place them in the directory of your choice. Set the current Matlab directory to that same directory, and run the sample GUI. This should give you a clear idea on how to apply this function to your own GUI.

One thing that should be noted is how the legend objects are stored into the handles structures. This can be seen in both the plotAxes1_pushbutton_Callback and plotAxes2_pushbutton_Callback functions in save_image_tutorial.m. This is crucial if you want to save the legend into the graphics file; otherwise, the saved plot will not have a legend.
End of tutorial.
21 Responses to “Matlab GUI Tutorial - Saving Plots within a GUI”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


Hi,
Thanks so much. I was really looking for this and it helped me a lot. You really are very good.
Hamsa
good…
Hallo,
Thank you for the code so much, but there is a small point the user must take care about which is: you supposed the units to be in pixels and that is not the case all the time. (in my case i have normalized units for the axes).
Regards,
sky
hey Sky,
You bring up a very good point!! Yes, that is an assumption that was made by me.
Quan
Great stuff!
What do you need to change to make this work for normalized units?
Thanks!
Hi,
the program is very good and is working well for one colomn data only.
could you please tell me how one can use code for data with five colomn for example?
Hi!
Thank you so much for this tutorials. i never used matlab in my life, but in a couple of hours you’ve helped me to programm a simply gui.
what do i have to change to make this work for normalized units?
Tee,
If you want to use normalized units, you will have to tweak some of the values within the code where the position property is being modified.
Quan
Te agradesco que compartas tus programas, de tal manera que tomo parte de tu codigo y lo adapto a mis necesidades. Muchas gracias, Alfredo H. D.
Hi there quan,
yesterday i was looking for the way to save image (from axes) in my GUI, then i found this tutorial and feel relieved. Just for info, the image i want to save is grayscale. After trying to modify your ‘SavePlotWithinGUI’ m-files, i can save the image, but the result is a color picture and it’s a bad one..
Then i remembered ‘imwrite’ that can save image, then i found by combining ‘uiputfile’ and ‘imwrite’ i can also save image within GUI.
But again there’s a problem because the result file is considered binary image (which i don’t expect).
Later i found another syntax that so helpful which is ‘uint8′, who can convert my image matrix to grayscale, exactly like i want to.
Hope whoever finds same obstacle can use this method and fix the problem..
Regards,
Denny
Thank you for the program, its simplicity and usability are commendable. One quick question though, I am using the program to read out a plotyy two axes graph and the command is writing over my second (right-hand) Y axis; this over right is effecting both the saved graph and the graph displayed in the GUI. Any suggestions? Thank you again.
really good. was very helpfull.
Greetz
Saving the plot is an important stage. Thank you for this tutorial and simplicity in description.
Hello all,
could u pless help me about to plot the effect of system response
to an exponential signal-gui for system response..It takes already about 1 month for me to search it, but i still do not find it.could u please give me the clue..tq
Hi! Thanks, so much for this!
I applied the function of the sample, but it showed that
??? Attempt to reference field of non-structure array.
Error in ==> save_image_tutorial>plotAxes1_pushbutton_Callback at 79
axes(handles.axes1)
Error in ==> gui_mainfcn at 95
feval(varargin{:});
Error in ==> save_image_tutorial at 42
gui_mainfcn(gui_State, varargin{:});
??? Error using ==> save_image_tutorial(’plotAxes1_pushbutton_Callback’,gcbo,[],guidata(gcbo))
Attempt to reference field of non-structure array.
??? Error while evaluating uicontrol Callback
I can not figure out what it said.
Hi… Tnx alot… that was great and has solved one of our problem!!!! ;))
Hi,
may I know… what is the coding to built a login page using GUIDE?
Hi,
may I know… can you show the coding to built a login page. Is it possible to do it using GUIDE?
Thanks… This saved my lot of time as I got a ready made piece of program to paste.
Hey QQ,
Is it possible to save them without displaying figure.. I mean any other way without having the figure pop up..
Thanks