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)?

13 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Apr 2013
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:
system(myFortranScript, -echo)
%This resets the environment variables back to default values of -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 Comment
A.B. on 16 Apr 2020
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.

Sign in to comment.

More Answers (0)


Find more on Fortran with MATLAB in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!