Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call engClose() from Matlab C API and wait for its process to be closed?

Tags:

c

matlab

I am using MATLAB C API to run some scripts and generate some Simulink models. The problem is that, when I finish the work with MATLAB, I call:

engClose(instance);

to close the MATLAB engine. But it seems that after calling the engClose method, MATLAB has still exclusive access to the generated files and I cannot use the files just after calling this method. Putting a sleep for 2-3 seconds after this method, fixes everything.

I checked the engClose documentation here. The documentation describes that:

This routine sends a quit command to the MATLAB engine session and closes the connection.

Does this description mean that the engClose method does not wait for the MATLAB process to be closed? If yes, is there any other way to wait for the MATLAB to be closed completely?

Update 1: My application has been developed with C++11, but this MATLAB interoperability part has written with C and MATLAB C API a long time ago. The target platform of the application is both Windows and Linux.

like image 851
Gupta Avatar asked May 18 '26 01:05

Gupta


1 Answers

After some days of investigation, I found that there is no standard way for closing Matlab synchronously. But what you can do is to try to close ALL the:

  1. open files (call fclose all to close all open files)
  2. classes and functions (call clear classes or clear functions, respectively. You can call clear all for clearing everything.)

before calling engClose.

In addition, make sure that you call rmpath(...)for every path that you have added to Matlab search path.

If you clean the Matlab process properly before calling its engClose method, then calling an asynchronous Matlab engClose will be fine.

like image 122
Gupta Avatar answered May 19 '26 13:05

Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!