如何从命令行调用MATLAB并在退出之前打印到stdout [英] How to call MATLAB from command-line and print to stdout before exiting

查看:334
本文介绍了如何从命令行调用MATLAB并在退出之前打印到stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在运行以下命令时,MATLAB打印输出



matlab -nodisplay -nojvm -rfprintf(1,'value:%f \ n',2.0); quit;



*注意:我目前运行的版本是7.10.0.499(R2010a)64位)

解决方案

-logfile 选项将所有输出的副本复制到文件中

  matlab -nodisplay -nojvm -logfile out.txt -rfprintf(1,'value:%f \\\
',2.0); quit;

Windows ,请使用 -wait 命令行选项阻止脚本的执行,直到MATLAB关闭。



Unix ,您可以使用 sleep 5s 睡眠5秒钟,或使用 wait 命令暂停执行,直到进程完成:

 #!/ bin / sh 
matlab -nodisplay -logfile out.txt -rrand(3),quit
wait $(ps | grep matlab | awk'{print $ 2}')&&& cat out.txt


I'm trying to run MATLAB scripts from command-line and am having problems getting MATLAB to return results to stdout.

When running the following command, MATLAB prints the license banner and exits without printing the message.

matlab -nodisplay -nojvm -r "fprintf(1, 'value: %f\n', 2.0); quit;"

*note: I am currently running Version 7.10.0.499 (R2010a) 64-bit (maci64)

解决方案

As was shown in this related post, you can use the -logfile option to make a copy of all outputs to a file.

matlab -nodisplay -nojvm -logfile out.txt -r "fprintf(1, 'value: %f\n', 2.0); quit;"

On Windows, use the -wait command-line options to block the execution of your script until MATLAB closes.

On Unix, you can use sleep 5s to sleep for 5 seconds, or use the wait command to pause execution until the process finishes:

#!/bin/sh
matlab -nodisplay -logfile out.txt -r "rand(3), quit"
wait $(ps | grep matlab | awk '{print $2}') && cat out.txt

这篇关于如何从命令行调用MATLAB并在退出之前打印到stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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