在matlab中打印多个数字

Mat*_* Kh 5 matlab printdialog matlab-figure

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

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
Run Code Online (Sandbox Code Playgroud)

但问题是,如果用户取消第一个数字的打印,我无法抓住它并取消其他打印.我该怎么做?

Hok*_*oki 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
Run Code Online (Sandbox Code Playgroud)
  • 保存文件。

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


适用于您的案例:

首先,请注意,在 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
Run Code Online (Sandbox Code Playgroud)

因此,在您的情况下,在调整后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
Run Code Online (Sandbox Code Playgroud)

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