MATLAB: Run exe with an input file and close upon process exit

exeexecutableinputMATLABnetsystemsystem command

Hi,
I am creating a GUI where the user is to input parameters, MATLAB then creates an input file (exp.in) that is to be read into an exe. Once the cfd computations have completed, I would like the exe to close itself (if it is successful). I have the following created using similar queries by others however do not know how to go about passing the exe an input file.
proc = System.Diagnostics.Process();
proc.StartInfo.FileName = 'P:\path...cfd.exe';
%proc.StartInfo.Arguments = 'P:\path...exp.in';
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
StreamWriter = proc.StandardInput;
sIn.AutoFlush = true;
sIn.write("dir P:\path...exp.in")
sIn.write("exit")
sIn.Close()
if isempty(proc)
error('Failed to launch process');
end
while true
if proc.HasExited
fprintf('\nProcess exited with status %d\n', proc.ExitCode);
break
end
fprintf('.');
pause(1.0);
end
Is there a way to pass on an input file for the exe to use. I have tried to use arguments but it doesn't work. Research suggests controlling process input via streamwriter.
I can use the system command to do so but then don't know how to close the exe once it finishes (have used taskkill to no avail; it closes before the process finishes).
Thanks in advance!

Best Answer

As I said in my comment, cfd.exe < inputfile.in does not pass any input argument to the executable. It's a completely different mechanism, and proc.StartInfo.Arguments = 'C:\inputFile.in'; is not equivalent to that at all. The .Net equivalent would be
proc.Start;
streamreader = System.IO.StreamReader('C:\inputfile.in');
proc.StandardInput.Write(streamreader.ReadToEnd);
With regards to &, I was too focused on the windows side and forgot that it's a matlab argument to mean that system should return immediately without waiting for the process to finish. So, if you want matlab to continue only when the process has finished then don't include that final &.
As to killing the process, if I understood correctly, you shouldn't have to do that as your program accepts a quit command that will tells it to terminate. It's a lot cleaner than killing it.
Related Question