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

查看:332
本文介绍了抑制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执行时

$ 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.

谢谢和问候! >

Thanks and regards!

推荐答案

您可以使用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)来指定outfile,以防止多进程环境中的冲突。

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日志matlab-log-1234.tmp。把它们在/ tmp如果你不想依赖pwd。您可以将其扩展为从单个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天全站免登陆