run a Matlab code in cmd and wait for it to finish

226 Views Asked by At

I want to run a matlab code in my c++ programme and wait for the results of Matlab and then continue to my c++ code. the problem is that the programme does not wait for matlab and continue their running. should I add anything to my code?

actually I tried to create process and used WaitForSingleObject.

int main()
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );


    system("matlab.exe -nosplash -nodesktop -nodisplay -r run('Main')");

    WaitForSingleObject( pi.hProcess, INFINITE );

    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );

.
.
.
}
1

There are 1 best solutions below

1
darune On

You probably need to use the option

-batch

to matlab instead of the option

-r

Eg. see this page for more detail: https://www.mathworks.com/help/matlab/ref/matlabwindows.html


Additionally the std::system call is 'standalone' - however you should check the return of the process launched.

int ret = std::system("....");
if (ret != 0) {
  std::terminate();//somethings not right...
}