MATLAB: How to fill an mxArray in a MEX-file for MATLAB

createdatafillinitializeMATLABmexmxarray

I would like to know all the ways I can fill an mxArray.

Best Answer

This change has been incorporated into the documentation in Release 2010a (R2010a). For previous releases, read below for any additional information:
Below is code that shows four valid methods for filling an mxArray with data:
 
#include"mex.h"
#define ONE plhs[0]
#define TWO plhs[1]
#define THREE plhs[2]
#define FOUR plhs[3]
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
const double data[]={2.1, 3.4, 2.3, 2.45};
double *dynamicData1, *dynamicData2, *dynamicData4;
double nonDynamicData[4];
const int dims[]={0,0};
int i;
/**********************************************************************************
METHOD 1, mxSetData with dynamic data
*********************************************************************************/
dynamicData1 = (double*)mxMalloc(4 * sizeof( double ));
// Create the matrix as 0 by 0, since you are about to allocate the memory manually
ONE = mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
for( i = 0; i < 4; i++ )
{
// Here, you could be loading data from somewhere.
// Instead, just copy from data[i]
dynamicData1[i]=data[i];
}
// Put the C array into the mxArray. You can do this because
// the mxArray is empty (0x0) at this time, so there will be no
// memory leak.
mxSetData(ONE, dynamicData1);
mxSetM( ONE, 2 );
mxSetN( ONE, 2 );
// It is not necessary to call mxFree( dynamicData1 ) because
// that memory is pointed to inside the mxArray you are returning.
/**********************************************************************************
METHOD 2, mxSetPr with dynamic data, mxSetPr is only for double data
*********************************************************************************/
dynamicData2 = (double*)mxMalloc(4 * sizeof( double ));
// Create the matrix as 0 by 0, since you are about to allocate the
// memory manually.
TWO = mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
for( i = 0; i < 4; i++ )
{
// Here, you could be loading data from somewhere.
// Instead, just copy from data[i]
dynamicData2[i]=data[i];
}
// Put the C array into the mxArray. You can do this because
// the mxArray is empty (0x0) at this time so there will be no
// memory leak
mxSetPr( TWO, dynamicData2 );
mxSetM( TWO, 2 );
mxSetN( TWO, 2 );
// It is not necessary to call mxFree( dynamicData2 ) because
// that memory is pointed to inside the mxArray you are returning.
/**********************************************************************************
METHOD 3, mxGetPr with non-dynamic data
*********************************************************************************/
// Create the matrix as 2-by-2, since you are just copying existing data into it
THREE = mxCreateNumericMatrix(2, 2, mxDOUBLE_CLASS, mxREAL);
for( i = 0; i < 4; i++ )
{
// Here, you could be loading data from somewhere.
// Instead, just copy from data[i]
((double*)mxGetPr(THREE))[i]=data[i];
}
/**********************************************************************************
METHOD 4, mxGetPr with dynamic data, using a copy of the data
*********************************************************************************/
dynamicData4 = (double*)mxMalloc(4 * sizeof( double ));
// Create the matrix as 2-by-2, since you are just copying existing data into it
FOUR = mxCreateNumericMatrix(2, 2, mxDOUBLE_CLASS, mxREAL);
for( i = 0; i < 4; i++ )
{
// Here, you could be loading data from somewhere.
// Instead, just copy from data[i]
dynamicData4[i]=data[i];
}
for( i = 0; i < 4; i++ )
{
// Copy the data in.
((double*)mxGetPr(FOUR))[i]=dynamicData4[i];
}
// You must call mxFree( dynamicData4 ) at this time because
// that memory is not pointed to inside the mxArray you are
// returning. This is because you copied it into the array with mxGetPr.
mxFree( dynamicData4 );
return;
}
The above example only uses the double data type, but you can also create mxArrays of other data type. The documentation has an example using unsigned 16-bit integers: