MATLAB: Access value of Enumeration from C/C++ mex function

enumerationmexobject-oriented

If I define an Enumeration in MATLAB as
classdef testEnum < int32
enumeration
Test0(0), ...
Test1(1)
end
end
and pass an instance of that enumeration
temp.a = testEnum.Test1;
mexEnum(temp)
to a C/C++ mex function, how do I access the integer value of the enumeration from within a C/C++ mex function?
For example compiling mexEnum.cpp with the code:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
const int dims[2] = {1, 1};
plhs[0] = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
double *temp = (double*)mxGetData(plhs[0]);
const mxArray *rhs = prhs[0];
mxArray *a = mxGetField(rhs, 0, "a");
int32_T *value = (int32_T *)mxGetData(a);
double valueDouble = (double)(*value);
*temp = valueDouble;
}
does not return the value (1) I would expect.

Best Answer

Old style obects (i.e., those using exclusively the @classname directory structure) were stored internally as structs, which made it easy to get at the data using mxGetField, mxGetFieldByNumber, etc. If you were using those objects they are easy to deal with in a mex routine.
New style classdef objects are stored in a completely different manner. The guts of the object are behind the Pr and Pi pointers, but exactly how this storage is arranged has never been publicized so you can't directly get at the data with official API functions. If it was a property you could use mxGetProperty (or the FEX submission mxGetPropertyPtr). But in the case of Enumeration there is no property for you to get. The only solution I can think of off the top of my head is to use mexCallMATLAB and convert it to something that you can deal with in a mex routine, e.g. a 32-bit integer. For example:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *mxi;
const int dims[2] = {1, 1};
plhs[0] = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
double *temp = (double*)mxGetData(plhs[0]);
const mxArray *rhs = prhs[0];
mxArray *a = mxGetField(rhs, 0, "a");
mexCallMATLAB(1,&mxi,1,&a,"int32");
int32_T *value = (int32_T *)mxGetData(mxi);
double valueDouble = (double)(*value);
*temp = valueDouble;
}
>> temp.a = testEnum.Test1
temp =
a: [1x1 testEnum]
>> testEnum_mexs(temp)
ans =
1