MATLAB MEX interface to a class object with multiple functions
Date: 2022-09-20 15:59:28
I am using the MEX interface to run C++ code in MATLAB. I would like to add several functions to MATLAB for handling a System
object:
sysInit() sysRefresh() sysSetAttribute(name, value) String = sysGetAttribute(value) sysExit()
Since each MEX dll can contain one function, I need to find a way to store the pointer to the global System
object which will exist until deleted by a call to sysExit
.
How can I do this in MATLAB properly? Are there any ways to store global pointers across calls to MEX functions?
Answers:
One common approach is to have several m-file functions that provide the public interface, e.g. sysInit.m, sysRefresh.m, etc.
Each of these m-files calls the mex function with some kind of handle, a string (or number) identifying the function to call, and any extra args. For example, sysRefresh.m might look like: