How can I pass a struct that contains different numeric arrays into a MEX function and have its fields accessed?

3 次查看(过去 30 天)
It is sought to use a struct in MATLAB that can be filled with an arbitrary number of numeric arrays as elements, while subsequently pass this array to a MEX-function and have all its fields accessed.
How can I pass a struct that contains different numeric arrays into a MEX function and have its fields accessed?

采纳的回答

MathWorks Support Team
This workflow can be achieved by using such MEX-API functions as, 'mxIsStruct' to check if a mxArray is a struct, 'mxGetNumberOfFields' to get the number of fields in case the mxArray represents a struct, 'mxGetFieldNameByNumber' to get the name of the field of a mxArray that represents a struct, 'mxGetFieldByNumber' to get the content in a particular element of a struct array of a particular field of a mxArray that represents a struct, and 'mxGetNumberOfElements' to get the number of elements that a mxArray has. As an example the following implementation of such a MEX-function is herein provided,
/*
* structArrays.c
*
* This is a MEX file for MATLAB.
*/
#include "mex.h"
#include "matrix.h"
/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
/* Print out the number of input arguments to the MEX-function */
mexPrintf("Number of input arguments %d\n", nrhs);
/* loop over all the input arguments to the MEX-function */
for (mwSize iEl = 0; iEl < nrhs; ++iEl) {
/* Get the mxArray corresponding to the current input */
const mxArray* my_el = prhs[iEl];
/* Check if the mxArray is a struct */
bool isStruct = mxIsStruct(my_el);
if (isStruct) {
/* Get the number of fields */
int numFields = mxGetNumberOfFields(my_el);
mexPrintf("Number of fields %d\n", numFields);
/* loop over all number of fields*/
for (mwSize iFields = 0; iFields < numFields; ++iFields) {
const char* namefield = mxGetFieldNameByNumber(my_el, iFields);
mexPrintf("Field name: %s\n", namefield);
/* Get mex-array */
const mxArray *my_arr = mxGetFieldByNumber(my_el, 0, iFields);
/* Check if mex-array represents a double field */
bool isDouble = mxIsDouble(my_arr);
if (isDouble) {
mwSize numEl = mxGetNumberOfElements(my_arr);
mxDouble *Value = mxGetDoubles(my_arr);
for (mwSize i = 0; i < numEl; ++i)
mexPrintf("%f ", Value[i]);
mexPrintf("\n\n");
}
}
}
}
}
Compile the latter mex-file using the following command,
 
mex structArrays.c -R2018a
where flag '-R2018a' is needed since API function 'mxGetDoubles' is used, see also the following documentation page for more information,
Then, please use the following MATLAB code for the proof of concept,
 
my_struct.arr1 = [1 2; 3 4];
my_struct.arr2 = [1 2 3; 4 5 6];
my_struct.arr3 = [1 2 3; 4 5 6; 7 8 9];
structArrays(my_struct)
where 'structArrays' stands for the name of the MEX-file hosting the latter implementation, to receive the following output,
 
>> my_struct.arr1 = [1 2; 3 4];
my_struct.arr2 = [1 2 3; 4 5 6];
my_struct.arr3 = [1 2 3; 4 5 6; 7 8 9];
>> structArrays(my_struct)
Number of input arguments 1
Number of fields 3
Field name: arr1
1.000000 3.000000 2.000000 4.000000
Field name: arr2
1.000000 4.000000 2.000000 5.000000 3.000000 6.000000
Field name: arr3
1.000000 4.000000 7.000000 2.000000 5.000000 8.000000 3.000000 6.000000 9.000000

更多回答(0 个)

标签

尚未输入任何标签。

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by