close cmd window after executable
显示 更早的评论
I am running an external .bat file through matlab. The code is as follows:
fileName = uigetfile('*.*');
cmd = sprintf(program.bat "%s" &', fileName);
system(cmd);
The user gets to pick which file is run through the .bat file and then it runs. The problem I am trying to solve, which I am not even sure it's possible, is to close the cmd window after it is done autonomously. The reason this is difficult is because after the program is done running, if you press enter, it prompts excel to open and displays the results in excel which I am trying to avoid. Thus, I don't think I can write a simple exit command. I also do not have access to the batch file to rewrite that code.
1 个评论
Jan
2016-11-24
How can you notice, that your external program is ready?
采纳的回答
更多回答(1 个)
Mikhail
2016-11-24
Much easier:
fileName = uigetfile('*.*');
cmd = sprintf('start program.bat "%s"', fileName);
system(cmd);
7 个评论
Walter Roberson
2016-11-24
That does not automatically close the command window after the program is done autonomously. It does not return to MATLAB until the program finishes.
Mikhail
2016-11-25
It does do both of these.
my.bat:
REM exit after 5 seconds
@ping 127.0.0.1 -n 5 -w 1000 > nul
exit
Then:
system('start my.bat')
Walter Roberson
2016-11-25
Your example exits after a few seconds. Until it does that, MATLAB is not given back control. The user is using a program that they want to run independently until it is time to kill it, and does not want the program to run to completion
Walter Roberson
2016-11-25
I did overlook the start. But your solution assumes that the process ends nicely by itself. In the user's case the process hangs around waiting for return to be pressed. Though I am not sure how it would read that if input is disconnected as I would expect when & is used, unless it is opening a graphics window.
Mikhail
2016-11-25
It the process doesn't exit by itself, wrap it in another bat file like that:
my.bat:
REM exit after 5 seconds
@ping 127.0.0.1 -n 5 -w 1000 > nul
my_wrapper.bat:
call my.bat
exit
Then:
system('start my_wrapper.bat')
That is all to it.
Walter Roberson
2016-11-25
Now try again with a process that does not exit by itself -- for example a ping that would go on forever until it is interrupted.
The user wants to be able to start an asynchronous process of indefinite duration and kill it at some later time before it has finished. The solutions I gave are various methods of killing asynchronous processes.
类别
在 帮助中心 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!