MATLAB: How to write a MATLAB MEX-file that requires the name of an mxArray now that the mxGetName function is deprecated

cdeprecatedlibrarymathMATLABmexmex filemexgetvariablemxarraymxgetnamewrite

I am writing a MEX-file application that writes a MATLAB variable to a proprietary file and uses the original variable name (name of the mxArray in the MATLAB workspace) as an identifier in the file structure. Now that the mxGetName function is deprecated, I can no longer get the variable's name for writing to the file.

Best Answer

To work around this problem, you must pass the variable name as a string argument to the MEX function. This way there would not be any need to get the variable's name in the MATLAB workspace. And the data can be copied from the MATLAB workspace using the mexGetVariable function.
Therefore, if for instance you previously called the MEX function by passing a variable:
sampleMEX(A);
You can modify your MEX-file so that you now pass the variable name as a string:
sampleMEX('A')
Then to get the data into the MEX file, you can use the mexGetVariable function:
mxArray *data = mexGetVariable("caller", mxArrayToString(prhs[0]));