Search code examples
matlabmex

Matlab Mex files with global data in C++


Good afternoon,

I'm hoping to use some external C++ libraries in my Matlab code to speed up some critical pieces. The issue is that the external libraries are spread across multiple files and make use of global data types. Can I use global data types with Matlab Mex files? Or is there a restriction on this? I've tried Googling this and haven't found any results on this issue.

Thanks so much.


Solution

  • Yes, global data can be used. The value of global variables is kept even when the MEX file is executed multiple times.

    Only when the MEX file is unloaded from memory it get reloaded on the next call to the MEX function. Then, and only then, the global variables are reset to their original value.

    The MEX file gets unloaded when you recompile the MEX file, when Matlab is closed, or when the MEX file is explicitly cleared by 'clear '.