0

I have a .exe file and want to run it through Matlab. I know that I should use !myfile.exe or system and/or evalc. However, after running the .exe file, it asks for input: Please enter the name of input file.

So, the question is that how should I declare the name of the input in my .m file?

8
  • 1
    I think we need more information for a definite answer. But if it is the .exe asking for a file name, you should be able to pass it as a parameter via the system call: !myfile.exe filename or (if you need the current file name) system(['myfile.exe ',mfilename('fullpath')]). Commented Jan 27, 2016 at 22:32
  • The problem is that the exe file does not have any default input file and this file should be typed ! So, I really don't know that how I should enter the name of the input file after running the exe file. Commented Jan 27, 2016 at 22:35
  • I'm not sure what you mean by "should be typed !" as the only difference, in my experience, is that the bang shell escape creates a literal line of system input while system allows for dynamic string input. That said, if the .exe doesn't accept the filename as a direct input, you'll have to manually enter the filename it needs via the Matlab command window I'm pretty sure. Commented Jan 27, 2016 at 22:41
  • Yes :), it works. How can I remove some of the comments that execute after running the .exe file? Commented Jan 27, 2016 at 22:51
  • In this case, assuming you needed to enter the filename directly, you either need to deal with the comments, or have the system spawn a shell by appending a & for the end of the system call; then enter the filename in that shell and all comments will be restricted to the instance. Commented Jan 27, 2016 at 23:00

1 Answer 1

1

Try the following scheme using the java.awt.Robot class.

r = java.awt.Robot;
system([myfile.exe ' &']); % dont forget to use the correct path if needed
pause(2) % allow some time for the computer to process
system([your_input_file_name ' &']);
r.keyPress (java.awt.event.KeyEvent.VK_ENTER); % press "enter" key
r.keyRelease (java.awt.event.KeyEvent.VK_ENTER); % release "enter" key

Another option is to use .net object if you are in windows by using SendWait and SendKeys, for example:

NET.addAssembly('System.Windows.Forms'); % import the .NET assembly
sendkey = @(strkey) System.Windows.Forms.SendKeys.SendWait(strkey) ; %useful function for the next lines:
%% now let's get started
system('myfile.exe &') ;   
pause(2);
sendkey('your_input_file_name')
sendkey('{ENTER}'); % press ENTER

credit to Luis Mendo and Hoki for telling me all about this

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.