如何在Matlab中从stdin读取 [英] How to read from stdin in Matlab

查看:86
本文介绍了如何在Matlab中从stdin读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长期运行的Matlab脚本来进行数据处理.我想在stdin上向其发送一个标志,以告诉它我有新数据要处理.我还想在处理完成后从stdout读取一个标志.

I have a long-running Matlab script for data processing. I want to send it a flag over stdin to tell it I have new data to process. I also want to read a flag from stdout when it is done processing.

换句话说,我有一个进程A,它大约每分钟发送一次标志到Matlab.我希望Matlab等待,直到它收到此标志.

In other words, I have a Process A that sends a flag about once a minute to Matlab. I want Matlab to wait until it receives this flag.

在matlab进程中写到标准输出就像调用fprintf一样容易.但是我如何从stdin读取? fopen 上的文档没有提及输入管道,也没有提及确实 fread .如何获得Matlab脚本从stdin中读取?

Writing to stdout in a matlab process is as easy as calling fprintf. But how can I read from stdin? Documentation on fopen doesn't mention an input pipe, and neither does fread. How can get a Matlab script to read from stdin?

推荐答案

实际上,该解决方案与input一样简单.将以下内容写入myscript.m:

It actually turns out that the solution is as simple as input. Write the following into myscript.m:

str = input('', 's'); 
fprintf(str); 
exit;

然后,在shell中运行以下命令:

Then, run the following in a shell:

echo Hello world | matlab -nosplash -nodisplay -nodesktop -r "myscript"

实际上,我们看到"Hello world"以及Matlab的启动文本被打印到控制台.

Indeed, we see that "Hello world" is printed to the console, along with Matlab's startup text.

因此,总而言之,input从stdin读取,而fprintf写入stdout.

So, in summary, input reads from stdin, and fprintf writes to stdout.

这篇关于如何在Matlab中从stdin读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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