How can I call a .com function through MatLab

I have a .COM file that can be run through the windows cmd prompt. This file takes 3 user inputs to run. I'd like to be able to run this function in a batch with per-determined inputs.
I've tried using the system and dos functions as follows:
system('myfile.com arg1 arg2 arg3')
system('"myfile.com" arg1 arg2 arg3')
dos('myfile.com arg1 arg2 arg3')
dos('"myfile.com" arg1 arg2 arg3')
These seem to put matlab in an endless loop. I know that the .com program take a short time to run, but when I attempt to call it in this way matlab just runs and I need to use 'ctrl + Break' to stop. Any advice would be appreciated.

2 个评论

Does it take the user inputs on the command line, or does it prompt for them?
To run the program I type the name into the cmd line and then get prompted 3 times to enter the 3 inputs

请先登录,再进行评论。

 采纳的回答

In MATLAB, create a file that contains the three input files, one per line. Then,
system(['myfile.com <' TheFileName])

11 个评论

This yields the same results (or lack there of) as my other attempts
How exactly is it prompting? If it is prompting in a graphics window, then you need to winopen() and then use the Java Robot Class.
It prompts though the cmd line:
C:\> Myfile.OUT
Yes no question: 'Y/N'
file name: 'file1'
file name: 'file2'
Is there a possibility of recompiling the source for the .COM as a .EXE ? My memory of how .COM work is somewhat tattered these days.
No, I don't have access to the source code only the .COM file
Tracing (without solution): .COM are MS-DOS 16 bit executables, which are emulated in MS Windows by the program ntvdm.exe (which is not present in 64 bit versions of MS Windows.) I do not seem to find information about whether I/O redirection is possible on the command line for those executables.
I would suggest experimenting with using the < I/O redirection at the Windows Command level; it would be useful to determine whether it works there but fails when system()'d.
The most practical approach might be to rewrite the program.
It looks like the < I/O redirection is not possible, running from the cmd line:
C: [Myfile.COM < inputs.txt] -> The system cannot find the file specified
C: ['Myfile.COM inputs.txt] - The system cannot find the file specified
C: Myfile.COM < inputs.txt -> Prompt for first input
C: [Myfile.COM arg1 arg2 arg3] -> '[Myfile.COM' is not recognized as an internal or external command, operable program or batch file.
C: Myfile.COM arg1 arg2 arg3 -> Prompt for first input
I don't think I can have the skill to rewrite the program, but I would like to know if this is impossible (seems to be the case) so we don't waste more time on it.
Thank you for all of you help.
The [] should not appear in your code. The C: probably should not appear but shouldn't hurt.
MyFile.COM < inputs.txt
or
C:MyFile.COM < inputs.txt
are the syntaxes I would expect to work.
The [] that I showed in the system() call are MATLAB-level array concatenation, and do not get passed to the command interpreter.
If C:MyFile.COM < inputs.txt is not working then the only other approach I can think of without getting into deep voodoo to implement redirection, would be to system("C:MyFile.COM &") and then use the Java Robot Class. You might have to winopen() cmd.exe instead of system() in order to get a proper command window running and waiting for input. There is a MATLAB File Exchange (FEX) demonstrating Java Robot.
Specifying the path brings up the cmd prompt waiting for inputs. Including < inputs.txt does not get the inputs in.
I am trying to use inputemu.m from the FEX, but I'm having trouble figuring out how to send the text to the cmd prompt.
>> system(inputemu('key_normal','n\enter'))
??? Error using ==> inputemu Too many output arguments.
system('"C:MyPath with spaces"'inputemu('key_normal','n'))
??? system('"C:MyPath with spaces"'inputemu('key_normal','n')) | Error: Unexpected MATLAB expression.
That approach is not going to work. Unfortunately I do not have time at the moment to explore.
okay thank you for your help

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Manage Products 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by