Pass commands to executable run through system command

39 visualizaciones (últimos 30 días)
Stephen Hall
Stephen Hall el 9 de Feb. de 2026 a las 18:14
Comentada: Walter Roberson hace alrededor de 19 horas
I have an executable provided by an external organization that I have opened with the system command, but I need to then automatically pass it inputs and my attempts to far have failed. The executable takes 4 inputs - the first and last are numeric, the middle two are text. I have tried packaging the inputs into a text file to no avail. Here is what it looks like (I've tried a bunch of things so this is not as clean as it started):
In1 = '1';
In2 = 'C:\Dir1\File1.dat';
In3 = '02/09/2026 16:00';
In4 = '0.5';
fileID = fopen('parameters.txt','w');
fprintf(fileID,'%s\r\n %s\r\n %s\r\n %s', In1,In2,In3,In4);
fclose(fileID);
system('C:\Dir2\main.exe < parameters.txt');
The function opens in the MATLAB command window but then just waits for me to manually input each input, and the whole reason I am doing this is to be able to run this multiple times for a bunch of variations, collect the data, and then process it and display it.
  1 comentario
Steven Lord
Steven Lord el 10 de Feb. de 2026 a las 18:51
Have you checked with the external organization that provided you the executable, to confirm that the executable is designed/intended to be run in a non-interactive environment rather than requiring interactivity? Does it require certain input arguments (like the -batch startup option for MATLAB) to bypass prompting for input from the user?

Iniciar sesión para comentar.

Respuestas (2)

Matt J
Matt J el 9 de Feb. de 2026 a las 18:29
Editada: Matt J el 9 de Feb. de 2026 a las 18:32
Perhaps as follows?
In1 = '1';
In2 = 'C:/Dir1/File1.dat';
In3 = '02/09/2026 16:00';
In4 = '0.5';
cmd = sprintf('C:/Dir2/main.exe %s "%s" "%s" %s', ...
In1, In2, In3, In4);
system(cmd);
  5 comentarios
Stephen Hall
Stephen Hall hace alrededor de 6 horas
No it does not. It ignores the additional inputs from a command prompt. When it executes from either MATLAB or command prompt it then prompts the user for each input individually, I am looking for a way for MATLAB to recognize that it is being prompted for input and then provide it.
Walter Roberson
Walter Roberson hace alrededor de 5 horas
Your executable appears to be bypassing standard input methods. Possibly .NET stdinput methods will work, perhaps not. If not, then there is not much you can do about the situation.

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 9 de Feb. de 2026 a las 20:03
You could use the .NET facilities, System.Diagnostics.Process . See the Question at https://www.mathworks.com/matlabcentral/answers/414795-run-exe-with-an-input-file-and-close-upon-process-exit for an outline of the code of passing in standard input.
  1 comentario
Walter Roberson
Walter Roberson el 11 de Feb. de 2026 a las 3:17
Your executable appears to be bypassing standard input methods. Possibly .NET stdinput methods will work, perhaps not. If not, then there is not much you can do about the situation.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by