I need to run a shell script from matlab by using system command (or unix, or !), but none of the FORTRAN executable files that I need actually works with "system", even though it still gives me status flag as 0, which usually means the code works well but it is obviously not the case for today. Has anyone ran into similar problems before? The shell script works perfectly fine otherwise. I never ran into such a strange problem with "system" command before, and I have not found anything vaguely related to this from the Internet. π
MATLAB: A peculiar problem when using system command to run a c shell script from matlab.
gfortransystem command
Related Solutions
Work-around from MathWorks...
Executing a GFORTRAN executable within a Apple terminal window is not equivalent to executing it within MATLAB. In particular, MATLAB will set various environment variables and this can have unexpected results which seems to be the case here.
To work around this issue you need to set your environment variables for the GFORTRAN-compiled programs correctly within MATLAB before you execute them. You can use the SETENV function for this purpose. Please execute your FORTRAN script as follows:
setenv(βGFORTRAN_STDIN_UNITβ, β5β) setenv(βGFORTRAN_STDOUT_UNITβ, β6β) setenv(βGFORTRAN_STDERR_UNITβ, β0β)!./forward < input setenv(βGFORTRAN_STDIN_UNITβ, β-1β) setenv(βGFORTRAN_STDOUT_UNITβ, β-1β) setenv(βGFORTRAN_STDERR_UNITβ, β-1β)
Note that after executing the FORTRAN script, I set the environment variables back to their default values.
For more information on the SETENV function, please refer to the following link:
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.
Best Answer