MATLAB有一個input
函數,用於監聽stdin
。請看下面的腳本,等待你的上游C程序進入它已寫入的數據塊的文件名,並刷新到磁盤:
while true()
% wait for upstream process to enter filename
filename = input('', 's');
% quit means we're done
if strcmp(filename, 'quit')
quit()
end
% run secondary processing as instructed
process_chunk(filename);
end
C程序可以簡單的寫一個數據文件的名稱stdout
。在寫完最後一個塊之後,它甚至可以通過說quit
告訴MATLAB腳本終止。實際上,C程序看起來可能如下所示:爲了使兩者協同工作,C程序必須「輸入」MATLAB進程。如果C程序被命名爲foo
和腳本是bar.m
,你把它們連在一起就像這樣:
$ foo | matlab -nojvm -nodisplay -r bar
我只是測試這對MATLAB R2013a(8.1.0.604),但我不會感到驚訝,如果input
有一直在聽stdin
很長一段時間,如果這在幾乎任何版本的MATLAB的Linux上都可以使用。
這就是所謂的脫氣,可以按[這裏](http://www-theorie.physik.unizh.ch/~dpotter/howto/daemonize)所述完成。 – Bort
如果Matlab會話/服務器具有與父進程不同的生命週期,則只需完全守護進程(將其從shell中分離)。目前還不清楚這是否需要從原來的描述。 – wrdieter
你也可以看看[這個答案](http://stackoverflow.com/questions/12306333/matlab-command-from-bash-command-line-on-an-already-running-session/12307838#12307838 )或甚至[這一個](http://stackoverflow.com/questions/18781803/call-a-function-by-an-external-application-without-opening-a-new-instance-of-mat) – thewaywewalk