假设我在程序中产生了几个数字。我想让用户可以选择一次打印所有内容。我不想为每个页面显示打印对话框。因此,我只显示一次,仅显示第一个数字。到目前为止,这是我提出的解决方案:

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 2013a/win 7。

为了使Matlab返回是否执行打印作业的值,您需要在print.m函数中插入一个小技巧。

入侵print.m

  • 找到print.m函数。它应该在..\toolbox\matlab\graphics\print.m周围的matlab安装文件夹中。
  • 找到后,进行备份! (此技巧很小,不应破坏任何东西,但我们永远不会知道)。
  • 打开文件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函数时,您都可以拥有一个包含信息的return参数。

    适用于您的情况:

    首先,请注意,在Windows计算机上,printdlg函数等效于使用print参数调用'-v'函数。
    因此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结构包含更多可重用的信息,包括打印作业选项,当前选择的打印机等...

    关于matlab - 在Matlab中打印多个图形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26284443/

    10-11 22:42
    查看更多