Querying data from enum arrays using C API
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am passing a struct as a block dialog parameter to an S-function. The struct is deeply nested and contains some fields that are enumeration arrays. The enums are defined in matlab and inherit from Simulink.IntType. I am able to grab the data fine when the field is a scalar, as in just one element. However, if the field is an enumeration array, then I cannot. For example, I have an enumeration array that in matlab shows size = [16x1]. I have a corresponding C type array of size 16, and type of the enumeration, also defined in C. I’m confused as to how to copy the data from the enum array since I’m getting rows =1, cols = 1, and numElements = 1 when running the following code.
My code is as follows:
const char *class_name = mxGetClassName(matlabArray); -> This returns the expected enumeration type name.
mxArray *enumArrayField = mxGetField(parentField, 0, “myEnumInstanceName”);
size_t rows = mxGetM(enumArrayField); %-> This returns 1
size_t cols = mxGetN(enumArrayField); %-> This also returns 1
size_t numElements = mxGetNumberOfElements(enumArrayField); %-> This also returns 1.
//
void *raw_data = mxGetData(enumArrayField); %->i tried to grab the data this way but doesn't seem to work because i cannot iterate on it
size_t element_size = mxGetElementSize(enumArrayField); %-> This returns 8 bytes
0 Kommentare
Akzeptierte Antwort
Shishir Reddy
am 27 Jun. 2025
Hi @MP
The error encountered is due to the variable you are querying for size and data. In your code, you are calling 'mxGetM', mxGetN, and mxGetNumberOfElements on matlabArray, but it should be on enumArrayField, which actually holds your [16x1] enumeration array.
size_t numElements = mxGetNumberOfElements(enumArrayField);
This should correctly give 16 elements if the field is [16x1].
As enumeration types in MATLAB are class objects, accessing the actual enum values won’t give usable numeric values directly. A common approach is to convert the enum array to its underlying numeric type using a call to MATLAB as follows -
mxArray *rhs[1] = {enumArrayField};
mxArray *lhs[1];
if (mexCallMATLAB(1, lhs, 1, rhs, "int32") == 0) {
int32_T *data = (int32_T *)mxGetData(lhs[0]);
for (size_t i = 0; i < mxGetNumberOfElements(lhs[0]); ++i) {
// copy data[i] into your C array
}
mxDestroyArray(lhs[0]);
}
For more information regarding 'mexCallMATLAB', kindly refer the following documentation -
I hope this resolves the issue.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Simulink Functions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!