Saving a Figure from inside a GUI

Hi all!
I have been designing a few GUIs to make some data analysis easier. At the end process however I want to be able to save the created plot (and legend) with a push button. I have been looking over past questions and have seen a few ways to approach the problem. My current method of saving the plot is as follows
cd('F:\JLAB\Gui_output')
picname=input('Enter Name for Image: ','s')
saveas(handles.ax, picname, 'jpg')
cd(handles.CF)
The problem with this is that I take the whole GUI window instead of just the plot. The two other methods I have seen are the export_fig function and the copyobj method. With the export_fig function I cannot figure out how to tell it to take only the part that I want. With the copyobj method (copy the plot to a figure window and save it there) I cannot figure out how to properly use the function.
Additional info: When I created the axes I defined them with
handles.ax=axes('Outerposition',[.25, 0,0.8,0.8]);
Cheers, Rhys

 采纳的回答

Image Analyst
Image Analyst 2013-6-14
With export_fig(), you can pass it the axes handle on your GUI if you want to save only the axes. Is that what you meant by "take only the part that I want"? Did you try to pass it the axes handles instead of the figure handle?

5 个评论

Yes that is what I meant by that. Sorry for not being clear. I thought I was passing the axes handles. Since I defined the axes as handles.ax that is what I would want to put into export figure correct. Perhaps when I tried export_fig I messed up something else. I will try this as soon as I have a chance and report back with the details. Thanks!
Try this demo, where I use export_fig() to save both the whole figure, and just one of the axes on it.
clc;
close all;
workspace;
fontSize = 16;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
handlesToAxes = subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
subplot(2, 2, 4);
imshow(rgbImage(:,:,2));
title('Green Channel Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Save the whole figure.
export_fig(gcf, 'Entire_figure.png');
% Save just the axes, not the whole figure.
export_fig(handlesToAxes, 'Just_the_axes.png');
Edit
******
I had not seen your comment before I posted this!
******
Now export_fig() almost does what I want it to do but it leaves out my points and the legend. , The way I have it plotted is
errorbar(x1,y1,'rs')
hold on
errorbar(x2,y2,'b*')
hold on
plot(x11,y11,'r--') % exponential fit for x1 y1
hold one
plot(x22,y22,'b--') % exponential fit for x2 y2
The exported image does not have the red squares or the blue stars, and the information on the legend is missing. Other than that it works like a charm!
Cheers!
I don't have your data so it's hard for me to reproduce.
Well it worked when I tried it outside of my program so I will just keep playing around with it. Thanks for your help, it is much appreciated!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by