Tim*_*Tim 14 matlab command-line
我想以非交互方式在bash中调用matlab,并在Matlab之外使用它的结果.
例如,我有一个脚本test.m
rand(3,4)
quit
Run Code Online (Sandbox Code Playgroud)
当我在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
Run Code Online (Sandbox Code Playgroud)
是否可以抑制Matlab的启动消息,并且只显示没有"ans ="的结果.
注意我不仅仅是针对这个例子提出一般性问题.
感谢致敬!
Amr*_*mro 11
尝试使用-logfile命令行选项:
Run Code Online (Sandbox Code Playgroud)-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
Run Code Online (Sandbox Code Playgroud)
此外,如果您在继续之前从需要它的脚本运行以完成运行,请使用-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.
Run Code Online (Sandbox Code Playgroud)
有关MATLAB启动选项的更多信息,请参见此处或matlab
可执行参考页:Windows/Unix
您可以使用Unix命令"tail + n"删除前n行输出.该标题看起来像10行,所以这将剥离它.
$ matlab -nosplash -nodesktop -nodisplay -r test | tail +10
Run Code Online (Sandbox Code Playgroud)
但是,这有点脆弱,因为警告(如"无窗口系统")将被剥离,并且标头大小将根据发生的警告而变化(这些警告是有用的诊断).此外,该警告可能在STDERR上而不是STDOUT上,因此"tail +9"可能就是您所需要的.
一种更强大的方法可能是修改Matlab脚本以使用fopen/fprintf/fclose写入单独的文件.这样,来自Matlab的标题,警告,错误等将与您想要的格式化输出分开.要使"disp"输出转到该单独的文件句柄,您可以使用evalc捕获它.可以使用-r消息中的test()参数和文件名中包含的$$ env变量(bash进程的PID)来指定outfile,以防止多进程环境中的冲突.
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);
Run Code Online (Sandbox Code Playgroud)
要从bash调用它,请使用此调用表单.(这里可能是次要的语法问题;我现在没有要测试的Unix盒子.)
% matlab -nosplash -nodisplay -r "test($$)" -logfile matlab-log-$$.tmp
Run Code Online (Sandbox Code Playgroud)
假设你的bash PID是1234.现在你已经输出了outfile-1234.tmp和Matlab登录matlab-log-1234.tmp.如果你不想依赖pwd,请将它们粘贴在/ tmp中.您可以扩展它以从单个matlab调用创建多个输出文件,如果您需要计算多个内容,则可以节省启动成本.
归档时间: |
|
查看次数: |
7576 次 |
最近记录: |