首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在MATLAB GUI上仅打印绘图

在MATLAB GUI上仅打印绘图
EN

Stack Overflow用户
提问于 2015-02-19 14:50:03
回答 2查看 880关注 0票数 1

如何仅将由MATLAB GUI创建的绘图打印到PDF文档中?

我知道网上有一个叫做export_fig的函数,但是我们不允许使用外部编码的工具来实现这个功能。

我目前有以下内容

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
function PrintButton_Callback(hObject, eventdata, handles)
set(gcf,'PaperType','A4','PaperOrientation','landscape','PaperPositionMode','auto');
print(get(handles.Axes,'Parent'), '-dpdf','Results.pdf');

然而,这会导致保存我的整个GUI图形。我怎样才能只点击由我的轴(“轴”)制作的图?

EN

回答 2

Stack Overflow用户

发布于 2015-02-19 16:49:02

print命令只接受figure作为handle参数...

要仅打印指定的轴,一个技巧是使用copyobj将该轴复制到新的临时图形,然后在新图形上使用print命令。

下面是一些示例代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
%% -- 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
EN

Stack Overflow用户

发布于 2015-02-19 16:11:37

打印前禁用轴可见性:set(gca,'Visible','off')

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28609347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文