MATLAB Answers

close cmd window after executable

50 views (last 30 days)
I am running an external .bat file through matlab. The code is as follows:
fileName = uigetfile('*.*');
cmd = sprintf(program.bat "%s" &', fileName);
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 Comment

Jan on 24 Nov 2016
How can you notice, that your external program is ready?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 27 Sep 2016
There are a few approaches:
  1. Java Robot Class to position on top of the command window and send the key/mouse sequence to close it;
  2. call taskmanager to kill the process
  3. instead of using system() use one of the activex / DCOM methods to create a process and send it a command line; you can then also kill off the process. I know this is possible, and I know someone showed how to create a process not long ago, but I am having difficulty finding the appropriate sequence at the moment.


Tyler Murray
Tyler Murray on 27 Sep 2016
Thanks for the start! Glad to know its possible haha I'll keep giving it a shot. If you remember the sequence please post it here!
Tyler Murray
Tyler Murray on 29 Sep 2016
Do you have any experience with Java Robot Class? I can't find anything online with how to position the cursor over the command window or any program?

Sign in to comment.

More Answers (1)

Mikhail on 24 Nov 2016
Much easier:
fileName = uigetfile('*.*');
cmd = sprintf('start program.bat "%s"', fileName);


Show 4 older comments
Walter Roberson
Walter Roberson on 25 Nov 2016
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 on 25 Nov 2016
It the process doesn't exit by itself, wrap it in another bat file like that:
REM exit after 5 seconds
@ping -n 5 -w 1000 > nul
call my.bat
system('start my_wrapper.bat')
That is all to it.
Walter Roberson
Walter Roberson on 25 Nov 2016
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by