The reason MATLAB is shutting down is that you are launching a process from within the MEX process. MATLAB is a single-threaded application and therefore the new processes that are launched are not independent of the main thread. When you launch the MEX function, it takes control of the MATLAB process (it does not launch an independent process). Likewise, when you launch the executable from within the MEX function, it does not actually get its own process, but lives within the MEX process that lives within the MATLAB process. Then, when your application exits, it is exiting the whole thread, including MATLAB.
Hopefully, this illustration will help. What you actually want is a control flow similar to the following:
MATLAB
||
||
|| MEX
|| ||
|| ||
|| ||
|| EXE
|| ||
|| ||
|| ||
|| EXIT (called by .exe)
||
||
But the actual control flow of the application is like this:
MATLAB
||
||
||
MEX
||
||
||
EXE
||
||
||
EXIT (called by .exe)
To help illustrate what is happening, if you run the MEX function below in MATLAB, the whole MATLAB process will exit:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{ exit(0); }
It appears as though you are using a routine that is making a call to 'exit()', or there is an explicit call to 'exit()', in your MEX code. This call will shut down the entire MATLAB thread.
To avoid this problem, search for and remove any instances of 'exit()' in your MEX code or use routines such as 'system()' that do not call 'exit()'.