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

5 views (last 30 days)
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 Comments
Shan Dou
Shan Dou on 15 Jul 2012
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?
Shan Dou
Shan Dou on 16 Jul 2012
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.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jul 2012
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.

More Answers (2)

Shan Dou
Shan Dou on 16 Jul 2012
Edited: Shan Dou on 16 Jul 2012
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.

Walter Roberson
Walter Roberson on 14 Aug 2012
Ben Ward posted a response from MATLAB showing how to work with this issue; see http://www.mathworks.co.uk/matlabcentral/answers/44388-or-system-or-unix-input-redirection

Community Treasure Hunt

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

Start Hunting!