1

I have an executable created with mcc. The .m file has a simple function that reads and plots values. After I run it from DOS, it freeze without returning execution to DOS. 2 questions: 1) How can i return execution to dos? I tried "return" and "exit" commands but didnt help 2) How to close the dos windows? is the only way to use a batch file or can I do it with a command in the .m file? thanks A.

Millemila
  • 1,612
  • 4
  • 24
  • 45
  • it looks like your program does not terminate. If your run it (not compiled) in Matlab, does the program return to the command window ? – Hoki Oct 21 '14 at 20:53
  • yes..................... – Millemila Oct 21 '14 at 21:00
  • I also added the line: disp('Plotting finished successfully!) as the last line of my m-file. It is displayed but nothing else happens (no return to prompt nor closing of dos window) – Millemila Oct 23 '14 at 09:08
  • Yes the program will not return to the console until the plot figure is closed. So if you run it from a DOS window, you will only get control after you close your last figure. Look at the `-e` parameter of the compiler to run your program without a DOS console. – Hoki Oct 23 '14 at 12:56
  • Oh great thanks!... u should answer so I vote it as best answer :) – Millemila Oct 23 '14 at 13:21
  • done, I added a few explanations to make it more complete. – Hoki Oct 23 '14 at 13:42

1 Answers1

2

There are 2 scenarii:

If your run your matlab executable from a DOS window, the DOS window will not get control back until the program terminates. If the program generate matlab figures (plot, surf, etc...), the program will not return to the console until all of the figures are closed.

You may think it is a waste for a simple plot, but after all your figure could be a evolved gui with a lot of code to execute. Or even a simple figure with a closeRequestFcn. So in Matlab terms, your program may still have instructions to execute as long as a figure is opened, so it will not return until it sure there is nothing more to do.

If you simply double clicked on your executable, the DOS looking console which open with your program will have the same behaviour. It will not disappear until the program returns (so until all your figures are closed if relevant).

I am not sure about linux, versions, but if you are running on windows, there is a way to suppress the DOS looking console for your graphic applications. Look at the -e switch in the mcc options. This switch will compile your program in a way that no DOS console is opened when you double click on your executable.

So to summarize, I would recommend:

  • If your program is a 'command line' type (a function that takes input from the console and return values to the same). => Compile with normal options, and execute it from a DOS window (you do not want the window to disapear as soon as the program terminates.)

  • If your program is a gui or even simple plotting functions, with no need for console interactions, then compile with the -e switch and execute it by double clicking the .exe file.

note that in case you use the -e switch, it is recommended to direct potential output to a logfile. Look at the mcc documentation for more info.


edit If you really need the DOS console and some graphic output, run your program form the command window with the following syntax:

start /b YourProgram 

This will start the program in "background mode" (use YourProgram & in Linux terminal). You will be able to do anything in this console window, and you will also see the output from your matlab executable.

It can be confusing because the output from your program will be added to the simple dos prompt and you may think you do not have the control but if you type any command it will work. You can even start many program this way and retain control in your console, but all the output will arrive in the same window and they may be difficult to differentiate.

Hoki
  • 11,637
  • 1
  • 24
  • 43
  • great answer. I knew about the -e switch, but thats not what I want, I want to see the dos console with the output on screen (i use disp command a lot), but I want to keep the figure open but still exit.. do you think there is a way to release the handle of the figure and leave it open and return execution? I.e. I want to dos window closed and figure open. thanks – Millemila Oct 23 '14 at 13:53
  • 2
    @Alberto, I completed the answer ;) – Hoki Oct 23 '14 at 15:40