【问题标题】:Printing Matlab Figures Using -nodisplay -nodesktop -nosplash使用 -nodisplay -nodesktop -nosplash 打印 Matlab 图形
【发布时间】:2012-06-19 22:33:56
【问题描述】:

我正在使用 Matlab 的 Mapping Toolbox 创建和打印北美的圆锥投影图。当我在 IDE 中运行代码时,绘图被正确打印和保存,但是当使用 -nodisplay -nodesktop -nosplash 在命令行上运行相同的脚本时,我遇到了一个非常奇怪的问题。

在 print() 函数调用期间,Matlab 停止运行脚本,没有任何错误、警告或崩溃日志。 Matlab 实际上并没有崩溃……它只是停止执行我的代码。假设可以打印不带显示器的图形according to this

其他人也遇到过类似的问题,并在MathWorks website 上询问过。

Here is some code to reproduce this problem.

到目前为止,还没有人提出解决方案。有没有人有什么建议?提前致谢!

编辑 1:

这里有一些独立的代码来重现问题。我在 R2011b 和 R2012a 上都进行了测试。

figure(1)
axesm eckert4; framem; gridm; axis off; tightmap

load geoid
contourfm(geoid, geoidrefvec, -120:20:100, 'LineStyle', 'none');

coast = load('coast');
geoshow(coast.lat, coast.long, 'Color', 'black')

contourcbar

print('-f1','-dpng','-r200','-painters', 'example');

【问题讨论】:

  • 您能否发一个short, self contained example,而不是要求我们先下载您的源代码的压缩文件,然后我们才能尝试回答您的问题。此外,链接到代码通常不是一个好主意,因为如果该链接损坏(比如您停止使用保管箱),那么这个问题可能毫无用处。
  • 我明白了。不幸的是,我制作的绘图是一个更大的软件包的一部分,但我会尝试提出一些独立的代码来重现它。
  • @GPSmaster:我假设您使用的是 Linux 机器。我在 WinXP 上的 MATLAB R2012a 中导出图形没有问题(尽管它警告你不要使用画家渲染器)
  • 是的,我正在使用 linux。我在 REHL5 和 ubuntu 10.10 上试过这个。为了澄清起见,您使用 -nodisplay -nodesktop 和 -nosplash 选项从命令行运行代码?因为如果我在 IDE 中运行相同的代码,我也可以导出图形。
  • @GPSmaster:是的。我将您的代码保存为myscript.m,然后运行:matlab -nodesktop -noFigureWindows -nosplash -r "myscript; quit"noFigureWindows 是 Windows 等效于 Unix-only nodisplay 选项)

标签: plot maps matlab matlab-figure


【解决方案1】:

如果您从脚本运行 MATLAB,则会出现以下警告,例如,

#!/bin/sh
nohup matlab -nodisplay -nodesktop -r myCode > myLog.log &
exit  

.

[Warning: Objects of graph2d.lineseries class exist - not clearing this class or
any of its superclasses] 
[Warning: Objects of scribe.legend class exist - not clearing this class or any
of its superclasses] 
[Warning: Objects of graphics.panbehavior class exist - not clearing this class
or any of its superclasses] 
[Warning: Objects of graphics.zoombehavior class exist - not clearing this class
or any of its superclasses] 
[Warning: Objects of graphics.rotate3dbehavior class exist - not clearing this
class or any of its superclasses] 
[Warning: Objects of graphics.datacursorbehavior class exist - not clearing this
class or any of its superclasses] 
[Warning: Objects of graphics.ploteditbehavior class exist - not clearing this
class or any of its superclasses]

问题是 matlab 代码想要显示图形、绘图或其他内容,但选项 -nodisplay 禁止它。我通过简单地将以下几行添加到我的代码set(gcf, 'visible','off'); 并在末尾添加close gcf; clear gcf; 解决了这个问题。现在情节图例与第一个情节一样,没有变化,我没有收到任何警告。

【讨论】:

    【解决方案2】:

    我得出的结论是这个问题是无法解决的,并且是一个错误。

    我最接近解决问题的方法是使用 shell 中的以下代码:

    $ matlab -nosplash -nodisplay < makefigure.m
    

    makefigure.m:

    plot(randn(100,1)); 
    set(gca,'Units','normalized','Position',[0.13 0.11 0.775 0.815]);
    set(gcf,'Units','pixels','Position',[4 4 1200 900]);  %# Modify figure size
    hgexport(gcf,'myfig.png',...
        hgexport('factorystyle'),'Format','png');
    

    这将输出一个 1200x900 像素的 png 文件“myfig.png”。不幸的是,虽然图像是我想要的大小,但图表本身仍然是小尺寸。我不确定这是什么原因,但我相信这与 Matlab 是面向对象的事实有关,并且轴应该与图形大小相关联(这就是gca 的“位置”变量被标准化为)。无论出于何种原因,这不会在显示器关闭时发生。我怀疑 Mathwoks 很快就会解决这个问题,我不能责怪他们,因为绝大多数 Matlab 用户都使用 GUI。

    可以帮助任何人解决此问题的一个潜在线索是,它在命令行上运行时会出错:

    Warning: Objects of graph2d.lineseries class exist - not clearing this class
    or any of its super-classes 
    

    我搜索了解决方案,但发现了更多问题。我的怀疑是,如果有人能弄清楚这意味着什么,你就可以解决这个问题。现在,我将再次回到 python,因为在使用 Matlab 时,我只是花几个小时处理不便,而不是提高工作效率。

    编辑: 如果有帮助,这是 Matlab 2012a Linux ... 2.6.35.6-45.fc14.x86_64 #1 SMP ... x86_64 x86_64 x86_64 GNU/Linux

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-12
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多