MATLAB: Is there a way to test the validity of an mxArray pointer from within a MEX-file in MATLAB

MATLAB

During the execution of my MEX file, I am interacting with variables in the MATLAB workspace through the MEX API functions. Consequently the size and even existence of MATLAB variables is subject to change during the execution of the MEX file.
Therefore I would like a way to test the validity of mxArray pointers in my MEX file so that I know they point to valid data in the MATLAB workspace.

Best Answer

The MEX API does not have a test to determine whether an mxArray pointer points to a variable that has been cleared from the MATLAB workspace.
Rather than storing an mxArray pointer that points to a workspace variable that may have changed, the safe thing to do would be to reacquire (through a call to mexGetVariablePtr) the mxArray pointer each time you make a callback to MATLAB that could clear variables.
For more information execute the following command in MATLAB
doc mexGetVariablePtr
Related Question