Matlab MEX create struct data type

I want creating MEX function in C/C++ and return struct data type to Matlab. This code can create struct data type with some of variable have int, double, string and matrix (array) data type. We need to use “mxCreateStructArray” matlab function to create struct data in C/C++ and return that struct data type to Matlab.

Please check my C/C++ code to create MEX with return struct data type in Matlab.

#include <string.h>
#include "mex.h"
#include <math.h>
#include "tmwtypes.h"
#define NARRAY  2
void fill_struct_data(char *string_message, char *separator, mxArray *plhs[])
{
    char *token;
    int iid, ival;
    double dval;
    char sval[30]; //accepting only string with maximum length 30 character
    static double dvalA[NARRAY];
    mxArray *mx_Array;
    
    /* get the first token */
    token = strtok(string_message, separator);
    while( token != NULL )
    {
        sscanf(token, "%i", &iid); /*get string id*/
        
        if(iid==0)  /*integer data*/
        {
            sscanf(token, "%i=%i", &iid, &ival);    /*get integer data*/
            mxSetField(plhs[0], 0, "int", mxCreateDoubleScalar(ival));  /*fill struct*/
        }
        else if(iid==1) /*double data*/
        {
            sscanf(token, "%i=%lf", &iid, &dval);   /*get double data*/
            mxSetField(plhs[0], 0, "double", mxCreateDoubleScalar(dval));  /*fill struct*/
        }
        else if(iid==2) /*string data*/
        {
            sscanf(token, "%i=%s", &iid, sval);     /*get string data*/
            mxSetField(plhs[0], 0, "string", mxCreateString(sval));  /*fill struct*/
        }
        else /*matrix or vector data*/
        {
            sscanf(token, "%i=%lf,%lf", &iid, &dvalA[0], &dvalA[1]);
            mx_Array = mxCreateDoubleMatrix(1, NARRAY, mxREAL);   /*allocate array*/
            memcpy(mxGetPr(mx_Array), dvalA, NARRAY*sizeof(double));    /*fill array*/
            mxSetField(plhs[0], 0, "array", mx_Array);
        }
        token = strtok(NULL, separator);
    }
    
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[])
{
    char *string_message, *separator;
    
    /*convert string input from matlab to C/C++ string data type*/
    string_message = mxArrayToString(prhs[0]);
    
    /*get string separator*/
    separator = mxArrayToString(prhs[1]);
    
    /*struct variable name */
    const char *field_names[] = {"int", "double", "string", "array"};
    mwSize dims[4] = {1, 1, 1, 1};
    
    /*create struct array data type*/
    plhs[0] = mxCreateStructArray(1, dims, 4, field_names);
    
    fill_struct_data(string_message, separator, plhs);
    return;
}

Save to mex_return_struct.cpp file (MEX with return struct data type). Compile from matlab command window with command :

mex mex_return_struct.cpp

Running our MEX with string parameter and string separator :

data = mex_return_struct('0=12 1=23.123 2=how_are_you 3=12.1,45.3', ' ')

Sample result :

data = 
       int: 12
    double: 23.1230
    string: 'how_are_you'
     array: [12.1000 45.3000]

As you can see, creating struct data type in C/C++ and call from Matlab is very simple.

Add a Comment

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.