How to run a .exe program from matlab and pass console input as an argument?
28 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jonathan Chase
am 5 Okt. 2023
Verschoben: Walter Roberson
am 7 Okt. 2023
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 &');
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 5 Okt. 2023
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 Kommentare
Walter Roberson
am 6 Okt. 2023
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
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Desktop finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!