MATLAB: How to access a single element of a structure array using the MATLAB mxArray interface in C

accesscopydealgetstructurelocateMATLABobtainstructurewith

I would like to extract a single element from a structure array using the MATLAB mxArray interface provided in C.

Best Answer

The ability to extract a single element from a structure array is not present using C MX-functions. To work around this issue, you can access sections of the structure array using the "mxGetData" command. An example of this follows:
mxArray **element_search
mxArray *mystruct;
mystruct = createarray();
element_search = (mxArray **)mxGetData(mystruct);
In the above code "mystruct" contains the contents of a MATLAB structure array. The function "createarray" populates the structure with data. The data found in the structure is an array containing variables of type "mxArray *". To access the fields of a specific element, you would access the variable using the syntax
element_search[field + element*numFields];
where "field" is the field number, "element" is the element number you wish to locate and "numFields" is the number of fields in each element.