How Can I Automate Input for an MRWE Application in MATLAB?

In summary, you run your executable in blocking mode, and you need to change the first line of your code to run in the background.
  • #1
natejensen
7
0
I have already referred to this posting:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241352
so I know the basics of running an executable from Matlab and inputing a file for it to run.

My problem is that because of the way my executable was written, I can only run the executable and not pass commands to it from Matlab. I have tried passing commands to the executable from the command window and that does not work either.

This works fine:
system('C:\...\executable.exe')
And the only thing any of these:
system(['C:\...\executable.exe<' input_file])
system(['C:\...\executable.exe ' input_file])
system(['C:\...\executable.exe <' input_file])
do is just run the executable.

When the executable is run, a "dos like" window pops up with a prompt to input the input file. From there I can manually enter in the input file, but that isn't really all that helpful if I want to run it 1000 times.

This "dos like" window is an MRWE Application Framework. I tried searching "matlab mrwe," but just came up with a bunch of dead links. It doesn't seem like absoft is supported by Matlab, but I just need to input a few lines of text. It shouldn't be too hard right?
 
Physics news on Phys.org
  • #2
Alright so I've been messing with that Java Robot Class stuff, and found this:
http://undocumentedmatlab.com/blog/gui-automation-robot/

I tried following their example but it's not working yet. This is what my code looks like now:

system('C:\...\executable.exe')
robot = java.awt.Robot;
robot.keyPress (java.awt.event.KeyEvent.VK_V);
robot.keyRelease (java.awt.event.KeyEvent.VK_V);
robot.keyPress (java.awt.event.KeyEvent.VK_E);
robot.keyRelease (java.awt.event.KeyEvent.VK_E);
robot.keyPress (java.awt.event.KeyEvent.VK_R);
robot.keyRelease (java.awt.event.KeyEvent.VK_R);
robot.keyPress (java.awt.event.KeyEvent.VK_ENTER);
robot.keyRelease (java.awt.event.KeyEvent.VK_ENTER);

When I run this, the code gets stuck on the first line. After the system command, the executable asks for an input file. It doesn't matter if I click on any other window (Matlab, the executable, my computer, whatever...) the code will not go to the second line until I either manually type in an input file, or close the executable.

I also tried switching the first two lines, but to no avail.

Any ideas?
 
  • #3
I know that I'm the only replying in this thread, so I don't know if anybody cares, but here is the answer.

This is because you run your executable in blocking mode. Instead, run your executable in the background:
system('C:\...\executable.exe &')

Note that the robot key-press require the target application to be in focus. You can do this by minimizing the Matlab desktop window:
com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame.setMinimized(true);

Yair Altman
http://UndocumentedMatlab.com
 
  • #4
Great work. I have ten data input files named (data1,data2,...,data10), Is it possible to make loop that consider the new name of input file??. In other way suppose we have something like :

for i =1:10
system('abcd.exe &');
robot = java.awt.Robot;
robot.keyPress (java.awt.event.KeyEvent.VK_D);
robot.keyRelease (java.awt.event.KeyEvent.VK_D);
robot.keyPress (java.awt.event.KeyEvent.VK_A);
robot.keyRelease (java.awt.event.KeyEvent.VK_A);
robot.keyPress (java.awt.event.KeyEvent.VK_T);
robot.keyRelease (java.awt.event.KeyEvent.VK_T);
robot.keyPress (java.awt.event.KeyEvent.VK_A);
robot.keyRelease (java.awt.event.KeyEvent.VK_A);
robot.keyPress (java.awt.event.KeyEvent.VK_i); -------------> change i to be 1,2,3,...10
robot.keyRelease (java.awt.event.KeyEvent.VK_i);-------------> change i to be 1,2,3,...10
robot.keyPress (java.awt.event.KeyEvent.VK_ENTER);
robot.keyRelease (java.awt.event.KeyEvent.VK_ENTER);
end
how to pass the file name as a variable?
 
  • #5


Based on the information provided, it seems like the issue lies with the way the executable was written, rather than with Matlab itself. It is possible that the executable was not designed to accept commands from an external program like Matlab.

One possible solution could be to modify the executable to accept commands from an external program. This may require consulting with the developer of the executable or hiring a programmer to make the necessary changes.

Another option could be to use a different method for running the executable, such as using the "system" function in Matlab to open the executable in a separate command window and then manually entering the input file. This may not be ideal for running the executable multiple times, but it could be a temporary solution while looking for a more efficient approach.

In general, it is important to ensure that the executable is compatible with Matlab and designed to accept commands from it before attempting to run it from Matlab. It may also be helpful to consult with other Matlab users or experts in the field for potential solutions or workarounds.
 

1. How do I run a .exe file from Matlab?

To run a .exe file from Matlab, you can use the system function. For example, if your .exe file is located in the current directory, you can use system('myprogram.exe') to run it.

2. How do I pass input arguments to the .exe file from Matlab?

You can pass input arguments to the .exe file by including them in the system function. For example, if your .exe file requires two input arguments, you can use system('myprogram.exe arg1 arg2') to pass them.

3. Can I capture the output of the .exe file in Matlab?

Yes, you can capture the output of the .exe file in Matlab by using the system function with the -output flag. This will store the output of the .exe file in a variable that you can then use in your Matlab code.

4. How do I check if the .exe file has finished running?

You can check if the .exe file has finished running by using the system function with the -wait flag. This will wait for the .exe file to finish running before continuing with the rest of your Matlab code.

5. Can I run a .exe file in the background while continuing to use Matlab?

Yes, you can run a .exe file in the background by using the system function with the -background flag. This will allow you to continue using Matlab while the .exe file is running in the background.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
967
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
Replies
3
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
22
Views
924
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Back
Top