How to run a .exe program from matlab and pass console input as an argument?

28 次查看(过去 30 天)
Hello,
I'm trying to run a precompiled c++ program within a matlab project using the 'system()' function. Calling the c++ program is striaghtforward and works fine (see below).
system('Fourier.exe &');
The issue I'm having is within the c++ program itself, which uses the 'getch()' function and requires user input from the keyboard to continue at a certain point within the program. Altering the source code and recompiling the program is out of the scope of the project work. I am just trying to automate this script so it will call the program and continue automatically, without needing console input from the user. This would allow the program to run in the background without monitoring it.
Is there a way to pass a user input from the keyboard as an argument to the 'system()' function, so that way when the c++ program reaches the line where it requires user input, it will just continue?
I have also tried including '< nul' (below) but that doesnt work either. Any help would be greatly appreciated.
system('Fourier.exe < nul &');

采纳的回答

Walter Roberson
Walter Roberson 2023-10-5
You are using Windows. In the case of MATLAB for Windows, instead of using system() for this situation, use .NET methods System.Diagnostics.Process
  5 个评论
Walter Roberson
Walter Roberson 2023-10-6
When you run the program interactively, is there action from the program as soon as you press the space key? Or is the action not until you press return?
If the action is not until you press return, then the program is using line-oriented reading, and is not notified of characters having been entered until a carriage return or new line are entered, at which point whatever had been typed so far would be forwarded to the program. In such a case, you would need to
stdin.Write(txt);
stdin.Write(newline);
or possibly
stdin.Writeline(txt);
If, however, the action is immediately upon pressing space, then the program is using single-character mode and does not need to be sent newline before it acts. However, for efficiency reasons, when you are dealing with streaming buffers between programs instead of dealing with keyboard I/O, at the time that you stdin.Write() what you Write() is not necessarily sent to the other side immediately. You might potentially need to
stdin.Flush()
to tell it to send what is in the buffers.
But sometimes none of these will work. Sometimes programs do not read from standard input, and instead connect directly to the keyboard drivers. If that happens to be the case for the executable you are dealing with, then anything you stdin.Write might simply be ignored by the executable. Potentially you might need to use the Java Robot class https://undocumentedmatlab.com/articles/gui-automation-robot
Jonathan Chase
Jonathan Chase 2023-10-6
Yes I see now, thank you for clarifying. This makes a lot of sense. To answer your question, the .exe continues on immediately after I hit the space bar.
Now I've updated the code slightly to include the Flush() method but it still does not write the space characters to the .exe. Seems like anything I give for standard input will not write to the command prompt when the .exe begins running.
process.Start();
txt = ' ';
stdin = process.StandardInput;
stdin.Write(txt);
stdin.Flush();
while(~process.HasExited)
pause(1);
end
try
process.Kill; % kill the process if needed.
catch
end
process.Close();
So I assume it is not going to read anything I give it using the standard input. Let me take a look at the link you provided.

请先登录,再进行评论。

更多回答(1 个)

Jonathan Chase
Jonathan Chase 2023-10-6
移动:Walter Roberson 2023-10-7
So I read up on some of the documentation you provided and ended up fixing the issue. As you mentioned the program wouldnt read any standard input. I solved the issue by invoking keyboard commands while within the Process loop.
process = System.Diagnostics.Process();
process.StartInfo.FileName = 'Fourier.exe';
process.Start();
robot = java.awt.Robot;
while(~process.HasExited)
pause(10);
robot.keyPress (java.awt.event.KeyEvent.VK_SPACE);
%
robot.keyRelease (java.awt.event.KeyEvent.VK_SPACE);
end
try
process.Kill; % kill the process if needed.
catch
end
process.Close();
Now the program is fully automated. Thank you for your time and for pointing me in the right direction.

类别

Help CenterFile Exchange 中查找有关 Desktop 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by