How do I create a structure array from a C MEX-file and pass it to MATLAB?

23 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 31 Oct 2016
Here is an example of a MEX-file that creates a MATLAB structure with two fields and passes it to the output argument when called with one. The MEX-file MYMEX should be able to create a structure that has two fields: a double array and a character array. Data is assigned to these and returned to the MATLAB workspace.
 
//This program creates a structure and returns it to MATLAB.
#include "mex.h"
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
//DATA
mxArray *mydouble,*mystring;
double *dblptr;
int i;
const char *fieldnames[2]; //This will hold field names.
//PROGRAM
if((nlhs!=1)||(nrhs!=0))
{
mexErrMsgTxt("One output and no input needed");
return;
}
//Create mxArray data structures to hold the data
//to be assigned for the structure.
mystring = mxCreateString("This is my char");
mydouble = mxCreateDoubleMatrix(1,100, mxREAL);
dblptr = mxGetPr(mydouble);
for(i = 0; i<100; i++)
dblptr[i] = (double)i;
//Assign field names
fieldnames[0] = (char*)mxMalloc(20);
fieldnames[1] = (char*)mxMalloc(20);
memcpy(fieldnames[0],"Doublestuff",sizeof("Doublestuff"));
memcpy(fieldnames[1],"Charstuff", sizeof("Charstuff"));
//Allocate memory for the structure
plhs[0] = mxCreateStructMatrix(1,1,2,fieldnames);
//Deallocate memory for the fieldnames
mxFree( fieldnames[0] );
mxFree( fieldnames[1] );
//Assign the field values
mxSetFieldByNumber(plhs[0],0,0, mydouble);
mxSetFieldByNumber(plhs[0],0,1, mystring);
//NOTE: mxSetFieldByNumber(..) will automatically take care
// of allocating required memory for the fields.
}//mexFunction(..)
  2 Comments
James Tursa
James Tursa on 26 May 2014
Edited: James Tursa on 26 May 2014
This comment is rather strange:
//NOTE: mxSetFieldByNumber(..) will automatically take care
// of allocating required memory for the fields.
To my knowledge, mxSetFieldByNumber does not allocate any memory for anything. The call will do the following:
1) Checks that the address passed to it came from the MATLAB Memory Manager (will abort with an ASSERT fault if not)
2) Sets the requested element spot of the struct to that address. (NOTE: Does not free anything already at that address ... so risks a permanent memory leak if there is already something there and you don't destroy it first)
3) Changes the Variable Type of the mxArray element to 3 (SUB-ELEMENT)
4) Removes the mxArray element from the garbage disposal Variable Array List.
There is no memory allocation involved in any of the above steps.
James Tursa
James Tursa on 26 May 2014
Also, this test is too restrictive:
if((nlhs!=1)||(nrhs!=0))
The user expects to be able to call most functions without setting it to an explicit left hand side variable. In such a case it simply goes into ans. Mex functions should generally allow this behavior also. So the above test would be better written as (with appropriate change in error message):
if((nlhs>1)||(nrhs!=0))
If nlhs is 0, MATLAB still reserves one spot for the output, so plhs[0] can be used in this case even though nlhs is 0.

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!