抑制 Matlab 的启动消息 [英] suppress start message of Matlab

查看:31
本文介绍了抑制 Matlab 的启动消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to call matlab in bash non-interactively and use its result outside Matlab.

例如,我有一个脚本 test.m

For example, I have a script test.m

rand(3,4)
quit

当我在 bash 中执行时

When I execute in 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=".

Is it possible to suppress the start message of Matlab and only show the results also without "ans=".

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

Note I am asking a general question not just for this example.

谢谢和问候!

推荐答案

您可以使用 Unix 命令tail +n"来删除输出的前 n 行.那个标题看起来像 10 行,所以这会去掉它.

You could use the Unix command "tail +n" to remove the first n lines of output. That header looks like 10 lines, so this will strip it.

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

不过,这有点脆弱,因为警告(如无窗口系统")将被剥离,标题大小将根据发生的警告而变化(这些警告是有用的诊断).此外,该警告可能在 STDERR 而不是 STDOUT 上,因此tail +9"可能就是您所需要的.

This is a little fragile, though, since warnings (like that "no window system") will get stripped, and the header size will vary depending on what warnings happen (and those warnings are useful diagnostics). Also, that warning might be on STDERR instead of STDOUT, so "tail +9" might be what you need.

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

A more robust approach could be to modify the Matlab script to write to a separate file using fopen/fprintf/fclose. That way the header, warnings, errors, etc from Matlab will be separated from the formatted output you want. To get the "disp" output to go to that separate file handle, you can capture it using evalc. The outfile could be specified using an argument to test() in the -r message, and the $$ env variable (the bash process's PID) incorporated in the file name to prevent collisions in a multiprocess environment.

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 机器.)

To invoke it from bash, use this calling form. (May be minor syntax problems here; I don't have a Unix box to test on right now.)

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

假设您的 bash PID 是 1234.现在您在 outfile-1234.tmp 中获得了输出,在 matlab-log-1234.tmp 中获得了一个 Matlab 日志.如果您不想依赖密码,请将它们粘贴在/tmp 中.您可以将其扩展为从单个 matlab 调用创建多个输出文件,从而在您需要计算多个事物时节省启动成本.

Let's say your bash PID is 1234. Now you've got your output in outfile-1234.tmp and a Matlab log in matlab-log-1234.tmp. Stick them in /tmp if you don't want to be dependent on pwd. You could extend this to create multiple output files from a single matlab invocation, saving the startup costs if you need to compute multiple things.

这篇关于抑制 Matlab 的启动消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆