Вы находитесь на странице: 1из 3


This tutorial will show you how to save an image, graph, or plot on a GUIs 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 GUIs Axes component. This tutorial is written for those with some experience creating a Matlab GUI. If youre 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 isant too outdated). Lets 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: 1. opens up a new figure 2. copies the contents of the axes object onto the new figure and adjusts the new figure 3. 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 4. saves the figure to either .emf, .bmp, or .fig file. (.jpg and .png were not used because Matlab is unable to display it properly) 5. closes the new figure Confused? Dont 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) 5axes_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.