Pass commands to executable run through system command

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 件のコメント

Steven Lord
Steven Lord 2026 年 2 月 10 日 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?

サインインしてコメントする。

回答 (3 件)

Matt J
Matt J 2026 年 2 月 9 日 18:29
編集済み: Matt J 2026 年 2 月 9 日 18:32

0 投票

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 件のコメント

Matt J
Matt J 2026 年 2 月 9 日 18:37
Or,
In1 = 1;
In2 = 'C:/Dir1/File1.dat';
In3 = '02/09/2026 16:00';
In4 = 0.5;
cmd = sprintf('C:/Dir2/main.exe %d "%s" "%s" %.3g', ...
In1, In2, In3, In4);
system(cmd);
Stephen Hall
Stephen Hall 2026 年 2 月 10 日 18:19
No joy. It runs the executable but then just waits for manual input instead of accepting the preloaded inputs.
Matt J
Matt J 2026 年 2 月 10 日 20:53
編集済み: Matt J 2026 年 2 月 10 日 20:53
Are you sure the executable takes the syntax you have described to us? What happens if you manually call the executable with the same 4 arguments outside of Matlab, from a Windows command prompt:
C:/Dir2/main.exe 1 "C:/Dir1/File1.dat" "02/09/2026 16:00" 0.5'
Stephen Hall
Stephen Hall 2026 年 2 月 13 日 20:59
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 2026 年 2 月 13 日 21:31
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.

サインインしてコメントする。

Walter Roberson
Walter Roberson 2026 年 2 月 9 日 20:03

0 投票

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 件のコメント

Walter Roberson
Walter Roberson 2026 年 2 月 11 日 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.

サインインしてコメントする。

Catalytic
Catalytic 2026 年 2 月 14 日 20:12

0 投票

Try this -
In1 = '1';
In2 = 'C:\Dir1\File1.dat';
In3 = '02/09/2026 16:00';
In4 = '0.5';
fid = fopen('parameters.txt','w');
fprintf(fid,"%s\n%s\n%s\n%s\n",In1,In2,In3,In4);
fclose(fid);
system('cmd /c ""C:\Dir2\main.exe" < parameters.txt"');

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2024b

質問済み:

2026 年 2 月 9 日 18:14

回答済み:

2026 年 2 月 14 日 20:12

Community Treasure Hunt

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

Start Hunting!

Translated by