MATLAB: ! or system() or unix() – input redirection

gfortraninput redirectionsystem command

I am trying to execute a system command from within Matlab (there is no input or output from/to Matlab, I just need it to call the program and wait).
./forward < input
This works fine in a bash terminal (MACI64), but not from Matlab. I have tried,
!./forward < input
system('./forward < input')
and
unix('./forward < input')
Each time, the input file is ignored.
(I also tried making a shell script containing the full command, but the extra input was still ignored. Also, if I change the name of the input file to one that doesn't exist (e.g. input_null, I get the following error: "/bin/bash: input_null: No such file or directory")
Is there any way to make this work?
thanks.
PS, simpler commands like
!grep -i string < input
work fine from Matlab

Best Answer

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:
Related Question