Documente Academic
Documente Profesional
Documente Cultură
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!
%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)
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.