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

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

问题描述

我正在尝试从命令行运行 MATLAB 脚本,但在让 MATLAB 将结果返回到标准输出时遇到问题.

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

运行以下命令时,MATLAB 会打印许可证横幅并退出而不打印消息.

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

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

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

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

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

推荐答案

如此处所示 相关帖子,您可以使用 -logfile 选项将所有输出复制到文件中.

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
', 2.0); quit;"

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

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

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

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 并打印到标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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