Responding to DOS prompt from MATLAB command line

14 views (last 30 days)
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

Accepted Answer

Fangjun Jiang
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?
  1 Comment
Ryan
Ryan on 5 Apr 2011
I finally discovered the problem thanks to your uigetfile functionality. When the GUI opened up to select a file, it started in the MATLAB "userpath" directory. Even though I was cd'ing to the proper directory in the command prompt (via MATLAB), it seems there was still some underlying functionality to have it search for files in "userpath". Even when navigating through the GUI in uigetfile, I could not open any other file that was not located in "userpath". I changed my userpath to the working directory with my .exe and .txt files and it all worked.
Thanks to everyone for the help. It all contributed to solving this issue.

Sign in to comment.

More Answers (3)

Walter Roberson
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
  1 Comment
Ryan
Ryan on 5 Apr 2011
Walter, thanks for the response. textInject itself worked as I had hoped, but I'm still having problems on my side. I posted a description as part of the response to Jason's comment below.

Sign in to comment.


Jason Ross
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
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.
Ryan
Ryan on 5 Apr 2011
The wrapper function does not work as I wanted it to in the Command Prompt. It shows the same behavior as if called in MATLAB (opens Notepad, but not hello.txt). For both MATLAB and Command Prompt attempts, I am executing from the same directory as hello.txt.
After this attempt, I'm thinking this might be some issue related to not being able to find the text file or not being able to interpret the file name. I also used Walter's suggestion with the textInject function, and had similar results, despite mirroring my own input from the Command Prompt.
Here is what I am inputting/seeing using the textInject function:
1. MATLAB command line: dos('cmd.exe &')
Response: Command Prompt window appears
2. MATLAB command line: textInject('C:\WINDOWS\system32\cmd.exe - cmd.exe',1,1,sprintf('cd C:\\Projects\\folder\\prgrm\\\r'))
Response: directory change to appropriate directory
3. MATLAB command line: textInject('C:\WINDOWS\system32\cmd.exe - cmd.exe',1,1,sprintf('prgrm.exe\r'))
Response: prgrm starts as expected, prompt for user input of text file name appears
4. MATLAB command line: >> textInject('C:\WINDOWS\system32\cmd.exe - cmd.exe - prgrm.exe',1,1,sprintf('filename.txt\r'))
Response: Command Prompt window shows following error: "Error using ==> textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
MATLAB:FileIO:InvalidFid"
and the prgrm terminates

Sign in to comment.


Fangjun Jiang
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
Ryan
Ryan on 5 Apr 2011
Fangjun, thanks for the response. Based on it's behavior, it looks like the prompt is just printing a message to the command line window. I don't think it's using uigetfile because there is no GUI selection box that appears. It looks like it is just taking the file name I enter and calling fopen afterwards.
When operating solely in the Windows command prompt window, I navigate to the directory that the executable is in, call the executable, and then just enter the file name after the prompt (text file exists in same directory as executable). I have also played around with variations on path entries along with the file name with no positive results.
Fangjun Jiang
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?

Sign in to comment.

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!