Responding to DOS prompt from MATLAB command line
14 views (last 30 days)
Show older comments
I am working to interact with a Windows executable that requires input only after being called. I have successfully called executables from MATLAB that required input along with the call, but this is a little different.
After calling the executable, the user has to wait for a prompt to enter a file name. Once the prompt appears, and the file name is entered, the program runs and completes. I can invoke the program without a problem, and MATLAB responds with the same prompt to enter the filename as it does in the Windows command prompt, but I am unable to respond to the MATLAB prompt successfully. The program responds with an error regarding an invalid file identifier (executable was built using MATLAB).
I am able to run the program in a command prompt window (outside of MATLAB), so I know the program itself works, but need to run these in batch mode and would be nice to do so in MATLAB.
I have tried several iterations using 'dos','system','!', and 'unix' commands with similar results every time. I have also tried including the file name along with the call to invoke the executable, but get a response that the script is not a function.
Any help on this issue would be greatly appreciated.
-Ryan
0 Comments
Accepted Answer
Fangjun Jiang
on 5 Apr 2011
Another approach with my example using File=input() works.
Assume the file name you entered through prompt is called MyText.txt.
Please create another text file first, the file is called input.txt, its content is a single line of text: MyText.txt
Run the following in Windows command line, no problem
TestCommandLine <input.txt
Run the following in Matlab command line, no problem
system('TestCommandLine <input.txt')
Can you try this and try the same with your executable?
More Answers (3)
Walter Roberson
on 4 Apr 2011
Are you trying to read via stdin from the executable back to Matlab? You can't do that. You could use Java Robot Class, or in a recent Answer (now deleted), Jan recommented textinject
Jason Ross
on 4 Apr 2011
You might want to try writing a batch file wrapper around the program you are calling.
For example, say that you can successfully use redirection in the DOS prompt to respond to your program as follows
program.exe > type response.txt
where program.exe is your program and response.txt contains what you are responding with (the filename, in your case).
You can create a batch file with the following in it (hello.bat): program.exe > type %1
and then within MATLAB, call system('hello.bat response.txt')
You could also use a similar technique with a perl script as well, if you are more comfortable using it, or if you might want more portable code.
Of course, the preferred method would be to change the code that makes the executable to accept command line input rather than requiring an interactive response, which is trickier to script and maintain.
The above example can be tried with notepad and text input file, e.g:
hello.txt: hello there MATLAB user
hello.bat: notepad > type %1
from MATLAB: system('hello.bat hello.txt')
Notepad will then open and have the contents of hello.txt in it.
YMMV with this technique, and it's highly possible that a more sophisticated process would give better results.
3 Comments
Jason Ross
on 5 Apr 2011
In the Command Prompt, does it work as you want it to using the wrapper? If it doesn't, there's no need to continue any further.
It would be helpful if you could post the exact code that causes the error to appear.
For the example, you must be in the same directory as hello.txt. I'm running a fairly vanilla Windows 7 install, but the command prompt / batch language hasn't changed significantly in some time.
Fangjun Jiang
on 4 Apr 2011
Do you know how the prompt (for user to enter a file name) is done? I tried an example using uigetfile. It seems to work both in Windows command window and Matlab command window.
Create a file called TestCommandLine.m
File=uigetfile
FID=fopen(File,'rt');
fclose(FID);
compile it using
mcc -m TestCommandLine
It creates a TestCommandLine.exe file.
Run TestCommandLine.exe in Windows command line, no problem.
Run system('TestCommandLine') in Matlab, no problem.
Maybe, pay attention to the path as well as the file name entered through the user prompt?
2 Comments
Fangjun Jiang
on 5 Apr 2011
I replaced the uigetfile() with the following line and it worked the same.
File=input('Please enter the file name:','s')
Can you try it to see if the prompt looks the same and whether this example works for you? The way you put every file in the same directory should eliminate any path problem. You do enter the file name with extension, don't you?
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!