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:
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.
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:
- open files (call
fclose all to close all open files)
- classes and functions (call
clear classes or
clear functions, respectively. You can call
clear all for clearing everything.)
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.
share|improve this answer
edited Sep 18 at 8:54
answered Sep 17 at 15:11
GuptaGupta 6,45544 gold badges2727 silver badges5252 bronze badges
clear doesn't close classes and functions. It just removes all variables from the current workspace.
clear functions clears functions from memory, and
clear classes clears variables, functions and class definitions. Neither of these options will make MATLAB release open file handles. Removing directories from MATLAB's path should no do this either. However, if you have a MEX-file or an M-file that holds on to resources (using
persistent variables, for example), then clearing those would help. But if they're
mlocked, you need to
munlock them before you can
clear them. – Cris Luengo Sep 17 at 16:01
fclose all will close all files opened with
fopen. – Cris Luengo Sep 17 at 16:03
@CrisLuengo Thanks for the comments. I applied them into my answer. Actually, in my implementation, I found that if I do not call
clear classes, my mexfiles cannot be moved by my program. So, I recommenced this step to do. In addition, I found that if a directory is in MATLAB path list, you can remove it by windows explorer, but you cannot remove it problematically!!! (e.g. using
QDir::rmdir). You can give it a check yourself. – Gupta Sep 18 at 8:49
@CrisLuengo I am using Matlab R2017b. – Gupta Sep 18 at 8:52
OK. Windows is weird... None of this would be necessary in Linux or MacOS. – Cris Luengo Sep 18 at 13:01
add a comment |