Pass commands to executable run through system command
30 次查看(过去 30 天)
显示 更早的评论
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
about 13 hours 前
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?
回答(2 个)
Matt J
2026-2-9,18:29
编辑:Matt J
2026-2-9,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);
3 个评论
Matt J
about 11 hours 前
编辑:Matt J
about 11 hours 前
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'
Walter Roberson
2026-2-9,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 个评论
Walter Roberson
about 5 hours 前
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!