A peculiar problem when using system command to run a c shell script from matlab.

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. :(

댓글 수: 6

Shan Dou
Shan Dou 2012년 7월 15일
편집: Shan Dou 2012년 7월 15일
It might have something to do with the verbose output of FORTRAN executable files. If I simply use:
**************************************
>>system( csh );
mycomputeraddress% fortranExe1 var1 var2
mycomputeraddress% fortranExe2 var1 var2
**************************************
Such a run, even when it appears to be conducted in a shell environment, still couldn't produce any output.
Only when I used the following steps:
***************************************
>>ssh -Y myworstationaddress
mycomputeraddress% fortranExe1 var1 var2
mycomputeraddress% fortranExe2 var1 var2
***************************************
In this way, the fortran codes work properly, but I have no idea how to do such a thing by using on system() call. Could anyone offer some suggestions?
Is there a reason you are not using
!fortranExe1 var1 var2; fortranExe2 var1 var2
or the system() equivalent? Both commands on the same line and no explicit invocation of the shell?
Do these executables perform graphics?
Thanks! I tried using ! and unix as well, but neither works directly inside of matlab command window. These executables do not perform graphics. They only have verbose stdout flashing in the command window while running so as to tell the users if everything is working properly. However, when trying to run them from matlab, matlab only quietly stays "busy" for one minute or so (quietly = NO warnings or error messages or any of those verbose stdout that I normally would see), then the command finishes executing, returns status=0 and result=' '(empty), but the output file that is supposed to have been written stays empty as well.
Another observation I have is that:
The executables I am having trouble with usually have help pages. For example, in a linux terminal, if I type "command -h", a few lines of text will be displayed explaining the basic usage. However, the same thing does not work in matlab no matter which one I use among !, system() and unix().
i.e. >>!command -h or >>system('command -h'); or >>unix('command -h');
None of the aforementioned methods displays the usage text. The returned status flag is always 0 and no error messages pop out from matlab command window. It is not a path problem either because I specified correct path.
Another observation is that with the software package from which I got those FORTRAN executables, all of its C executables work normally when called inside of matlab (they work well with !, system() and unix()), while none of its FORTRAN executables works (and no error messages would show up) with matlab calls.
Any suggestions are more than welcome! Thank you very much!
I wonder... this output, is it maybe being sent to stderr instead of stdout ? Help information is often sent to stderr.
Uh huh! You are right! I just took a look at the source codes and the associated file descriptor for write statement is indeed 0 (UNIT=0 for stderr). Why would matlab not display the help info in this case?
My boss just suggested me to try different FORTRAN compilers with "Hello World" code, and it turns out that the one I compiled with gfortran does not work with system command, but if I use g77 instead, it works well. What might cause such a problem? Does matlab have any version requirement for FORTRAN compilers or hidden library issues that I did not notice? I am using matlab R2012a.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2012년 7월 16일
I am going to speculate a bit here:
Possibly gfortran links against a different libc than g77 does, and the only g77 uses is the one provided with MATLAB.
If this speculation is correct then you should be able to determine it by downloading and applying "Dependency Walker".
The libc provided with MATLAB would know how to communicate with MATLAB's stderr and stdout.
If my speculation is correct then using g77 would be easiest. You might be able to convince gfortran to link against an alternate libc but my recollection is that gfortran needs some of the facilities in Microsoft's libc.

추가 답변 (2개)

Shan Dou
Shan Dou 2012년 7월 16일
편집: Shan Dou 2012년 7월 16일
Thanks! :)
I tried the same thing with R2011b, and both versions of the "Hello World" executables work well. My current guess is that some library incompatibility (esp. runtime library, as my boss pointed out for me) issues exist between gfortran and R2012a, but I am so relieved that R2011b works well with fortran executables that were compiled with gfortran.
I don't know if any system dependency exists and thus my case only represents a special situation, but if such a problem is prevalent, it might be something that needs to be fixed for R2012a in future.

카테고리

도움말 센터File Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by