【问题标题】:suppress start message of Matlab抑制 Matlab 的启动消息
【发布时间】:2009-10-05 02:48:01
【问题描述】:

我想以非交互方式在 bash 中调用 matlab 并在 Matlab 之外使用它的结果。

比如我有一个脚本test.m

rand(3,4)
quit

当我在 bash 中执行时

$ matlab -nosplash -nodesktop -nodisplay -r test
Warning: No window system found.  Java option 'MWT' ignored

                        < M A T L A B (R) >
              Copyright 1984-2008 The MathWorks, Inc.
                     Version 7.7.0.471 (R2008b)
                         September 17, 2008


  To get started, type one of these: helpwin, helpdesk, or demo.
  For product information, visit www.mathworks.com.


ans =

0.8147    0.9134    0.2785    0.9649
0.9058    0.6324    0.5469    0.1576
0.1270    0.0975    0.9575    0.9706

是否可以抑制 Matlab 的启动消息,并且只显示结果也没有“ans =”。

请注意,我问的是一个一般性问题,而不仅仅是针对此示例。

感谢和问候!

【问题讨论】:

  • 在 Octave 中,您可以使用 -q 标志。

标签: matlab command-line


【解决方案1】:

尝试使用 -logfile 命令行选项:

-logfile log         - Make a copy of any output to the command window
                       in file log. This includes all crash reports.

然后您可以使用任何您想要的方式轻松删除前几行(例如 sed)。示例:

matlab.exe -nosplash -nodesktop -nojvm -logfile out.log -r 'rand(3,3), exit'
sed '1,5d' out.log

此外,如果您是从需要它在继续之前完成运行的脚本运行的,请使用 -wait 选项:

-wait      - MATLAB is started by a separate starter program
           which normally launches MATLAB and then immediately
           quits. Using the -wait option tells the starter
           program not to quit until MATLAB has terminated.
           This option is useful when you need to process the
           the results from MATLAB in a script. The call to
           MATLAB with this option will block the script from
           continuing until the results are generated.

有关 MATLAB 启动选项的更多信息可以在 here 或 matlab 可执行参考页面中找到:Windows/Unix

【讨论】:

【解决方案2】:

您可以使用 Unix 命令“tail +n”删除前 n 行输出。该标题看起来像 10 行,所以这将删除它。

$ matlab -nosplash -nodesktop -nodisplay -r test | tail +10

不过,这有点脆弱,因为警告(如“无窗口系统”)将被删除,并且标题大小会根据发生的警告而变化(这些警告是有用的诊断)。此外,该警告可能在 STDERR 而不是 STDOUT 上,因此您可能需要“tail +9”。

更可靠的方法是修改 Matlab 脚本以使用 fopen/fprintf/fclose 写入单独的文件。这样,来自 Matlab 的标题、警告、错误等将与您想要的格式化输出分开。要使“disp”输出转到该单独的文件句柄,您可以使用 evalc 捕获它。可以使用 -r 消息中的 test() 参数指定输出文件,并在文件名中包含 $$ 环境变量(bash 进程的 PID)以防止多进程环境中的冲突。

function test(ppid)
outfile = sprintf('outfile-%d.tmp', ppid);
fh = fopen(outfile, 'w');
myvar = rand(3,4);
str = evalc('disp(myvar)');
fprintf(fh, '%s', str);
fclose(fh);

要从 bash 调用它,请使用此调用形式。 (这里可能是轻微的语法问题;我现在没有 Unix 机器可以测试。)

% matlab -nosplash -nodisplay -r "test($$)" -logfile matlab-log-$$.tmp

假设您的 bash PID 为 1234。现在您的输出在 outfile-1234.tmp 中,Matlab 日志在 matlab-log-1234.tmp 中。如果您不想依赖 pwd,请将它们放在 /tmp 中。您可以扩展它以从单个 matlab 调用创建多个输出文件,如果您需要计算多个事物,则可以节省启动成本。

【讨论】:

  • 这不应该是tail -n +10,加上-n吗?
  • 取决于tail 的版本。没有-n 的语法是一个较旧的变体,在某些地方仍然受支持,包括我工作的 OS X。我使用它是因为我很懒并且习惯了它。包含-n 会使其更便携,并且可能更具可读性。
【解决方案3】:

我建议将输出保存到文件中,然后读入该文件。这种方法稍微复杂一些,但随着格式的变化等不那么脆弱。它给了你更多的控制权。您会在网络上找到大量用于将 Matlab 文件转换为不同宿主语言的脚本。

例子:

A = randn(3, 2);
save temp_output.mat A
# Later, read temp_output.mat in whichever language you desire.

【讨论】:

    【解决方案4】:

    要抑制ans =的显示,可以使用DISP函数:

    disp(rand(3,4));
    

    要禁止显示第一条警告消息,您可以尝试添加选项 -nojvm 以查看是否有帮助。

    要抑制其他所有内容,您可以从解决相同问题的 MathWorks 新闻组线程中尝试 this solution。

    【讨论】:

    • -nojvm 不会抑制启动消息,即“ ...”
    • @Tim:那是一个错误的输入。我的意思是建议它可能有助于警告信息。我会解决的。
    【解决方案5】:

    这样调用 MATLAB

    matlab -nodisplay  <test.m &>matlab.output
    

    会将所有启动消息和其他显示的输出转储到 matlab.output 文件(可以任意命名)。如果您随后(按照彼得的建议)让 test.m 使用将您需要的结果保存到文件中

    csvwrite('temp_output.txt',A)
    

    或其他适当的输出函数,然后您可以在此文件中读取并继续。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      相关资源
      最近更新 更多