System Command Output not Appearing in Command Window
Show older comments
Hi all,
I've been battling with the system command recently and am absolutely stumped. I am running on Mac OS X 10.7, MATLAB 2012a (7.14) and in the bash shell. I have an external script that I can successfully run from MATLAB, but the intermediate output from this script is not echoed back to the command window. I have tried system('MyCommand','-echo') with no success. When I run it in Terminal, I get all kinds of intermediate info, such as convergence criterion, residuals, etc., but this is not the case when I run the script via MATLAB.
I'm absolutely stumped and have no idea why the '-echo' flag isn't working. Any thoughts?
Thanks!
8 Comments
Walter Roberson
on 16 Jul 2012
Maybe this is related to http://www.mathworks.com/matlabcentral/answers/43640-a-peculiar-problem-when-using-system-command-to-run-a-c-shell-script-from-matlab
Different platform, but release is the same.
Thomas
on 17 Jul 2012
Can you post your bash script?
Parker
on 2 Mar 2013
Hi,
I am witnessing similar behavior (R2012b) on a Windows platform.
With previous versions of Matlab, everything was fine - all console output was adequately echoed back to the command window in all cases (whether the program ended normally or with an error).
Now, when the program ends on an error, the last output flush is not echoed to the command window (all previous output flushes are still echoed normally).
This is extremely bothering as that means you're systematically deprived of the latest chunk of information, obliterating any chance of knowing what went wrong just by looking at the output.
I've seen the similar topic http://www.mathworks.co.uk/matlabcentral/answers/44388-or-system-or-unix-input-redirection, and tried to change the environment variables accordingly (either by setting them in the Windows System configuration panel or by using setenv in matlab before calling the executable), but I haven't managed to make any progress.
Does anyone have an idea on how to fix this ?
Outputting the console to an intermediate file is unacceptable as I need to be able to see and check what's happening as the program progresses.
The only way I can think of would be to compile with an option to flush all output after every line, but I'd rather find another way if possible (ideally with 100% normal behavior matching).
Thanks
Walter Roberson
on 3 Mar 2013
Does the lost output eventually come out, or does it just disappear?
I suggest experimenting with
fprintf(1,'\n')
fprintf(2,'\n')
to send output to stdout and stderr respectively.
I just tried using the formal POSIX mechanism of fseek(1,0,'cof') and fseek(2,0,'cof') but those returned error statuses (with no message available to ferror)
Parker
on 4 Mar 2013
No, the lost output never appears.
Let's say I were to write the following program and and compile it with gfortran :
PROGRAM TEST
print*,'hello'
x=0.
y=x/x
END
When reaching the dividing by 0 line, the program will crash (if the -ffpe-trap flag is activated, but that's not the point, as the problematic behavior appears with any kind of crash), and nothing will have nor will be echoed back to the Matlab command window.
If I were to write
PROGRAM TEST
do i=1,1000
print*,'hello',i
enddo
x=0.
y=x/x
END
Then only the last hundred lines or so would be missing (simply put, lines that have been already flushed before the crash are fine). Hence the workaround of calling
setenv('GFORTRAN_UNBUFFERED_ALL','1')
before calling the program as it ensures all lines are flushed one at a time, making sure you'll get all the output. The problem isn't fixed though... Also, it's basically impossible to follow output that way with 50 lines per second or so are appearing on screen.
Regarding your tests, I'm not sure I follow. Do you want me to type that in Matlab ? I just tried and both did work fine, correctly sending a newline to the command window, but that's to be expected, no ? Otherwise it would not be a gfortran-only issue but would concern every compiler out there...
Walter Roberson
on 4 Mar 2013
If you run the fortran program outside of MATLAB do the final values appear?
Yes. But to be fair, it is flushing output line by line, so there couldn't have been any surprises there.
In any case, the point is everything was fine with previous Matlab releases...
Also, the "workaround" to flush each line independently as I presented previously is actually quite a bad idea, as it significantly slows down execution time if you also write things in text files.
It is far better to use
setenv('GFORTRAN_UNBUFFERED_PRECONNECTED','1')
But this is still only a workaround.
Answers (0)
Categories
Find more on Startup and Shutdown 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!