5

I am trying to write a MATLAB script that would call and run an external program and then proceed with other MATLAB commands.

tic                       %Start stopwatch
system('MyProgram.exe')   %Call and run my program
toc                       %End stopwatch 

However, this program "MyProgram.exe" requires me to "Press Enter to Exit." How to make my MATLAB script pass "Enter" to proceed? Like How to pass "Enter" as an input of my program at the end of execution? Or how to do this in general ?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
ASE
  • 1,702
  • 2
  • 21
  • 29
  • It could be as simple as passing the second return to the system call : `system('Myprog.exe\n')` – Brydon Gibson Jun 28 '17 at 20:19
  • It will not move to the next line before pressing "Enter". How can MATLAB pass this "Enter" so that the script will continue without user interference ? – ASE Jun 28 '17 at 20:22
  • 2
    On unix, you can use `system('MyProgram < /dev/null')`. On window, I guess you should be able to use `system('MyProgram.exe < NUL')`, but I'm not able to test it for the moment. – m7913d Jun 28 '17 at 20:57
  • 4
    If your program is on the command line and not GUI-based, you can probably `echo.|MyProgram.exe` – SomethingDark Jun 28 '17 at 21:04
  • Thank you very much m7913d and SomethingDark . Both solutions work. – ASE Jun 28 '17 at 21:09

2 Answers2

6

On UNIX, you can use

system('MyProgram < /dev/null'). 

as suggested in the Matlab documentation:

To disable stdin and type-ahead redirection, include the formatted text < /dev/null in the call to the invoked command.

The Windows equivalent is (based on this post):

system('MyProgram.exe < NUL')
m7913d
  • 10,244
  • 7
  • 28
  • 56
4

When a console program needs to take input one time from the user and there is no built-in way to do so (like passing it in as an argument), that input can be echoed and piped to the program. This can also be used to press Enter (again, once) by piping a blank line.

echo.|program.exe

While traditionally a blank line is generated with echo by using the command echo., this can fail if the current directory contains a file called echo that has no extension. To get around this, you can use a ( instead of a ..

echo(|program.exe
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • I seem to recall the Dostips thread discussing alternatives to `echo.` eventually finding a case where `echo/` didn't work. – SomethingDark Jun 29 '17 at 11:05
  • Yes, `echo/` fails in case any echoed text begins with `?`; I believe this is the only error case... – aschipfl Jun 29 '17 at 11:57