在Matlab中打印多个图形

5
假设我的程序会产生几个图片,我想给用户提供一次性打印它们的选项。我不想为每一页显示“打印对话框”。因此,我只显示一次对话框,并仅对第一张图片进行显示。这是我目前想到的解决方案:
figHandles = get(0, 'Children');
for currFig = 1:length(figHandles)
    if(currFig == 1)
        printdlg(figHandles(currFig)); % Shows the print dialog for the first figure
    else
        print(figHandles(currFig)); % Does not show the print dialog and uses the latest printer selection
    end
end

但问题在于,如果用户取消了第一张图的打印,我无法捕捉并取消其他的打印。我该怎么做才能解决这个问题呢?


基于旧版Matlab的源代码 - 不确定它是否能返回任何内容,因为它使用eval进行打印。 - Cheery
这是一个非常有趣的问题,但也许您应该修改标题以显示您想要检索打印作业的状态。 - Hoki
1个回答

1

好的,这是一个相当卑劣的技巧,肯定不能保证适用于所有版本。但是在我使用的Matlab 2013a / win 7上有效。

为了让Matlab返回它是否执行了打印作业的值,您需要在print.m函数中插入一个小的黑客程序。


修改 print.m 文件

  • 定位 print.m 函数。它应该在您的 Matlab 安装文件夹中的 ..\toolbox\matlab\graphics\print.m 附近。

  • 一旦找到,请备份文件! (这个技巧很小,不应该会有什么问题,但我们永远不知道)。

  • 打开 print.m 文件并找到 LocalPrint(pj); 行,它应该在主函数的末尾附近 (~240行)。

  • 将该行替换为:

.

pj = LocalPrint(pj); %// Add output to the call to LocalPrint
if (nargout == 1)
    varargout{1} = pj ; %// transfer this output to the output of the `print` function
end
  • 保存文件。

完成了黑客攻击。现在每次调用print函数时,您都可以获得一个充满信息的返回参数。


适用于您的情况:

首先,请注意,在Windows机器上,printdlg函数等同于使用'-v'参数调用print函数。
因此,printdlg(figHandle)print('-v',figHandle)完全相同。'-v'代表verbose。我们将使用它。

print函数的输出将是一个结构体(我们称之为pj),其中包含许多字段。要检查打印命令是否实际执行,您需要检查的字段是pj.Return

pj.return == 0 => job cancelled
pj.return == 1 => job sent to printer

所以在你的情况下,在对print.m进行调整后,它可能看起来像这样:

pj = print('-v',figHandles(1)); %// Shows the print dialog for the first figure
if pj.Return %//if the first figure was actually printed (and not cancelled)
    for currFig = 2:length(figHandles)
        print(figHandles(currFig)); %// Does not show the print dialog and uses the latest printer selection
    end
end

注意: pj 结构包含许多可重复使用的信息,包括打印作业选项、当前选择的打印机等等...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接