11

I'm trying to run MATLAB scripts from command-line and am having problems getting MATLAB to return results to stdout.

When running the following command, MATLAB prints the license banner and exits without printing the message.

matlab -nodisplay -nojvm -r "fprintf(1, 'value: %f\n', 2.0); quit;"

*note: I am currently running Version 7.10.0.499 (R2010a) 64-bit (maci64)

Amro
  • 123,847
  • 25
  • 243
  • 454
papahabla
  • 1,448
  • 18
  • 18
  • 1
    possible duplicate of [How can I stop MATLAB from returning until after a command-line script completes?](http://stackoverflow.com/questions/14495/how-can-i-stop-matlab-from-returning-until-after-a-command-line-script-completes) – gnovice Jan 06 '11 at 02:55
  • The issue with 'How can I stop Matlab from returning until after a command-line script completes?' is related to how Windows opens separate command window to execute commands without blocking terminal. Also, the use of the -wait command-line option which was the solution to that issue doesn't exist on non Windows systems. – papahabla Jan 06 '11 at 19:05

1 Answers1

8

As was shown in this related post, you can use the -logfile option to make a copy of all outputs to a file.

matlab -nodisplay -nojvm -logfile out.txt -r "fprintf(1, 'value: %f\n', 2.0); quit;"

On Windows, use the -wait command-line options to block the execution of your script until MATLAB closes.

On Unix, you can use sleep 5s to sleep for 5 seconds, or use the wait command to pause execution until the process finishes:

#!/bin/sh
matlab -nodisplay -logfile out.txt -r "rand(3), quit"
wait $(ps | grep matlab | awk '{print $2}') && cat out.txt
Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454