MATLAB: How to start Matlab from command prompt and wait for the application to return

command promptMATLAB

I'm trying to run some memory analysis tool to debug a MEX file and detect possible heap corruption.
I need to start Matlab from this tool, and type the script commands corrupting the memory. It is not possible to attach to an existing process since the tool need to know the original application state.
The issue is that matlab.exe creates a new process and return immediately. So the analysis is complete before it can even start.
How can I make matlab.exe return when matlab is closed?

Best Answer

Matlab actually has a command line option -wait that does the trick.
matlab.exe -wait
Or with running scripts automatically as Jan Simon suggested
matlab.exe -wait /r "C:\MFiles\YourScript"