Running MATLAB with a shell script, redirecting stdin and stdout

11 ビュー (過去 30 日間)
Elias Khalil
Elias Khalil 2013 年 3 月 23 日
Let's say I have this MATLAB function
function a = testfun(in1, in2)
a=in1+in2;
disp(a);
I would like to have a shell script that runs on Cygwin as follows:
./testscript.sh < inputforfunction.txt > outputoffunction.txt
The input file would contain in1 and in2. After running this command, the output file will contain the result of running testfun(in1,in2), namely 3 if in1=1 and in2=2, for example.
What I have till now is:
#!/bin/sh
matlab -nodesktop -nosplash -r "try, testfun($1,$2); end; quit;"
Executing the below opens MATLAB's command window, prints 40, then exits.
./testscript.sh 23 17
However, I don't know how to redirect the input to a text file, save the printed output of the MATLAB function somewhere, then redirect it to the output file.
Thanks!

回答 (2 件)

Robert Cumming
Robert Cumming 2013 年 3 月 23 日
  1 件のコメント
Elias Khalil
Elias Khalil 2013 年 3 月 23 日
Hi Robert. Thanks for this, it inspired me to use -logfile which is similar. Still haven't got to make the redirection work though.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2013 年 3 月 23 日
If you want to accept inputs from a file that has been redirected, then you need to use input() to read from the file.
  2 件のコメント
Elias Khalil
Elias Khalil 2013 年 3 月 23 日
Hey Walter, thanks. I understand that "input(msg)" asks for user input, how can this be used to read input from a file and pass it to a MATLAB function in a shell script?
Walter Roberson
Walter Roberson 2013 年 3 月 23 日
編集済み: Walter Roberson 2013 年 3 月 23 日
#!/bin/sh
matlab -nodesktop -nosplash -r "try; vs = input('','s'); v =sscanf(v, '%f'); testfun(v(1),v(2)); end; quit;"
OR
#!/bin/sh
read in1 in2
matlab -nodesktop -nosplash -r "try; testfun($in1,$in2); end; quit;"

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by