Save GUI axes with legend to jpg

1 次查看(过去 30 天)
My code is able to save the axes1 plot but legend is missing on the saved file. Any help to get legend in the saved image? Any other method appreciated. I do not want to use export_fig. Using Matlab 2014a.
[filename,pathname] = uiputfile('*.jpg;*.png;*.tif','Save as');
if pathname == 0 %if the user pressed cancelled, then we exit this callback
return
end
haxes=handles.axes1;
ftmp = figure('visible','off');
new_axes = copyobj(haxes, ftmp);
set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
saveas(ftmp, fullfile(pathname,filename));
delete(ftmp);

回答(1 个)

Philip Caplan
Philip Caplan 2015-4-13
This is expected behavior because the axes object does not save an associated legend. You will need to make a separate call to "copyobj" on a legend to copy a legend to your new figure. The following code snippet illustrates this (toggle the "copyLegend" variable to determine whether the legend is copied):
close all;
copyLegend = true;
plot(1:10);
hleg = legend('myplot');
hax1 = gca;
ftmp = figure;
new_axes = copyobj(hax1,ftmp);
if copyLegend
copyobj(hleg,ftmp);
end
A legend will only appear on Figure 2 if "copyLegend" is set to true.

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by