How can I run an .exe from Matlab that requires user input?

72 views (last 30 days)
I need to run an executable called program.exe that waits for the user to provide an input (write a file name and press enter). If I write
system('cd path_to_exe\program.exe')
the executable header text starts appearing in the Matlab console, but pauses midway indefinitely and I have to Crt+C to quit the program. Writing
system('cd path_to_exe\program.exe < NUL:')
displays the whole program.exe header, but then I am unable provide the necessary input ('file.txt'). Writing
!start path_to_exe\program.exe
opens the .exe in a cmd console, but I am unable to provide the input ('file.txt') from Matlab. How can I run the .exe and provide the text input from Matlab?
  1 Comment
Jan
Jan on 21 Feb 2018
The "cd" seems to be not useful here. If you want to start a program, use system('path_to_exe\program.exe') without "cd".

Sign in to comment.

Answers (1)

Bob Thompson
Bob Thompson on 20 Feb 2018
Before you run the .exe create an ascii file (text file is fine) which contains the inputs you need for the .exe. Make sure you include \n to simulate pressing 'enter'.
executefile = fopen('executefilename.txt','w');
fprintf(executefile,strcat(fileinput,'\n',input1,'\n',etc.,'\n'));
fclose(executefile);
system('cd path_to_exe\program.exe < executefile');
  2 Comments
Alfonso Velencoso Gomez
Alfonso Velencoso Gomez on 11 Oct 2018
This is utterly useful Bob. Thank you.
But I think there is something wrong in your last line of code. It should be like this:
system('cd path_to_exe\program.exe < executefilename.txt');
SARA OUANES
SARA OUANES on 21 Sep 2019
Hello Alfonso,
I tried your code but it did not work because it is not the right syntax for 'system' command. This one worked Fine for me. Note that both files are in my working directory.
[status,cmdout]=system('ExecutableFileName FileName.format');

Sign in to comment.

Categories

Find more on Manage Products 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!