如何仅将MATLAB GUI创建的绘图打印到PDF文档中?
我知道在线提供的名为export_fig
的功能,但我们不允许使用外部编码工具。
我目前有以下
function PrintButton_Callback(hObject, eventdata, handles)
set(gcf,'PaperType','A4','PaperOrientation','landscape','PaperPositionMode','auto');
print(get(handles.Axes,'Parent'), '-dpdf','Results.pdf');
然而,这导致我的整个GUI图形被保存。 如何仅选择轴(“轴”)的绘图?
答案 0 :(得分:1)
print
命令仅接受figure
作为handle
参数...
要打印指定的轴,只有一个技巧是使用copyobj
将此轴复制到新的临时图形,并在新图形上使用print
命令。
以下是一些示例代码:
%% -- Test code
function [] = TestPrint()
%[
% Create figure with two axes
fig = figure(1); clf;
ax1 = subplot(1,2,1);
plot(rand(1, 12));
ax2 = subplot(1,2,2);
plot(rand(1, 12));
% Print the whole figure
print(fig, '-dpdf', 'figure.pdf');
% Print ONLY second axis
printAxis(ax2, '-dpdf', 'axis.pdf');
%]
end
%% --- Print specified axis only
% NB: Accept same arguments as 'print' except for first one which now is an axis.
function [] = printAxis(ax, varargin)
%[
% Create a temporary figure
visibility = 'on'; % You can set it to off if you want
tempFigure = figure('Visible', visibility);
cuo = onCleanup(@()clearTempFigure(tempFigure)); % Just to be sure to destroy the figure
% Copy selected axis to the temporary figure
newAx = copyobj(ax, tempFigure);
% Make it fill whole figure space
set(newAx, 'OuterPosition', [0 0 1 1]);
% Print temporary figure
print(tempFigure, varargin{1:end});
%]
end
function [] = clearTempFigure(h)
%[
if (ishandle(h)), delete(h); end
%]
end
答案 1 :(得分:0)
打印前禁用轴可见性:set(gca,'Visible','off')
。