Why does the output of my FORTRAN script not show up in the MATLAB command window when I execute it using the SYSTEM function in MATLAB 7.14 (R2012a)?

24 ビュー (過去 30 日間)
I am running MATLAB 7.14 (R2012a) and interfacing with an external FORTRAN script on a Macintosh. The code was compiled on my local box with GFORTRAN. I can successfully run the script with the bang (!), UNIX, or SYSTEM function.
The issue is that the code periodically outputs text and I want that information echoed back to the command window. I try the '-echo' option with the SYSTEM function and the command window in MATLAB does not display the output.
The issue does not exist with MATLAB 7.11 (R2010b). When I run the exact same code through MATLAB 7.11 (R2010b), all of the desired text is displayed to the command window.

採用された回答

MathWorks Support Team
MathWorks Support Team 2013 年 4 月 15 日
This happens starting from MATLAB (7.14) R2012a onwards, where Environment variables for the three data streams STDIN, STDOUT and STDERR were set to -1.
To work around the issue, you may wrap your SYSTEM call which executes a FORTRAN script as follows:
setenv(GFORTRAN_STDIN_UNIT, 5)
setenv(GFORTRAN_STDOUT_UNIT, 6)
setenv(GFORTRAN_STDERR_UNIT, 0)
system(myFortranScript, -echo)
%This resets the environment variables back to default values of -1.
setenv(GFORTRAN_STDIN_UNIT, -1)
setenv(GFORTRAN_STDOUT_UNIT, -1)
setenv(GFORTRAN_STDERR_UNIT, -1)
In any program, there are three reserved streams that the program can read from and write to.
‘stdin’ is generally what is used as the input stream, such as when a program asks you to type something on the keyboard.
‘stdout’ is generally what is used as the output stream, such as when you use the WRITE function in FORTRAN.
‘stderr’ is generally what is used as the error stream, which is where a program will write the issues it is encountering, so that a developer or debugger can differentiate between output that is supposed to be seen and errors in the program that are discovered.
GFORTRAN-compiled programs attempt to open these three streams, and then unconditionally close them when they are finished. However, this is an issue for programs like MATLAB which interferes with the input and output of user processes which also read to and write from ‘stdin’, ‘stdout’, and ‘stderr’.
Therefore, from MATLAB (7.14) R2012a onwards, these three environment variables are set to -1 by default. Setting this to -1 will ensure that MATLAB has control over these streams, thereby allowing it to function correctly. While running a FORTRAN program which needs this access from within MATLAB, these changes have to be made temporarily.
When these variables are set to -1, note that MATLAB will not read from the ‘stdout’ and ‘stderr’ streams the FORTRAN script expects to write to, and it will not write to the ‘stdin’ stream the script expects to read from. This will cause the output of the program to not be captured in the command window.
  1 件のコメント
A.B.
A.B. 2020 年 4 月 16 日
I have a dylib on macOS that prints output to stdout. It is an Intel-compiled-Fortran project, linked to a matlab MeX C file. However, the stdout is not displayed on the command window. On Windows, this was easy to solve by opening a Console application (although that is not technically matlab command window). Thanks for your help in advance.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFortran with MATLAB についてさらに検索

製品


リリース

R2012a

Community Treasure Hunt

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

Start Hunting!

Translated by