我想打电话给MATLAB在bash以非交互并使用其结果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 =”。
请注意我问一个一般性的问题不只是这个例子。
感谢致敬!
你可以使用Unix命令“尾巴+ N”删除输出的第n行。 该标头看起来像10行,所以这将去除它。
$ matlab -nosplash -nodesktop -nodisplay -r test | tail +10
这是一个有点脆弱,不过,因为警告(类似“无窗系统”)将获得剥夺,而头大小将取决于警告发生的事情(和这些警告是有用的诊断)而有所不同。 此外,该警告可能会在STDERR,而不是STDOUT,所以“尾巴+9”可能是你所需要的。
更可靠的方法可以修改MATLAB脚本编写使用的fopen / fprintf中/ FCLOSE一个单独的文件。 这样的标题,警告,错误等从MATLAB将你想要的格式输出分开。 要获得“DISP”输出到那个单独的文件句柄,你可以使用EVALC捕捉到它。 outfile中可以用一个参数来测试()的-r消息中指定,并且在文件名中掺入的$$环境变量(在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的日志1234.tmp您的输出。 他们坚持在/ tmp目录,如果你不希望依赖于PWD。 你可以扩展这从一个单一的MATLAB调用创建多个输出文件,大大节省了启动成本,如果你需要计算多的东西。
尝试使用-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的启动选项的详细信息,可以发现在这里 ,或在matlab
可执行参考页: 的Windows / Unix的
我建议你保存输出到一个文件,然后读取该文件。 作为格式转换等它给你更多的控制这种方法稍微复杂一些,但不那么脆弱。 你会发现很多网页上的脚本的Matlab的文件转换为不同的主机语言。
例:
A = randn(3, 2);
save temp_output.mat A
# Later, read temp_output.mat in whichever language you desire.
为了抑制的显示ans =
,您可以使用DISP功能:
disp(rand(3,4));
为了抑制第一警告通知,您可以尝试添加的选项-nojvm
,看看是否有帮助。
为了抑制一切,你可以尝试这种解决方案从MathWorks公司新闻组线程解决同样的问题。
调用MATLAB这样
matlab -nodisplay <test.m &>matlab.output
将转储的启动消息和其它显示输出到matlab.output文件(可以被命名为任何你想要的)的。 如果再(以下彼得的建议)有test.m保存您使用需要文件的结果
csvwrite('temp_output.txt',A)
或其他适当的输出功能,您可以在这个文件中读取并继续进行。