MATLAB: How to copy data to a new string variable in a C-MEX file

apiarrayccharcppMATLABpointervalue

I have created a string variable in my C-MEX file using mxCreateString. I would like to copy the content to a new string variable.

Best Answer

Use the mxArray* returned by the mxCreateString function to copy data to a new mxArray* as follows:
#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
mxArray *origString, *userString;
int arrayLen;
/* Initialize */
origString = mxCreateString("This is the primitive string");
/* Copy values to new string */
userString = origString;
/* Determine array length */
arrayLen = mxGetNumberOfElements(origString);
printf("The array length is: %d \n", arrayLen);
/* Remaining code is just for testing */
plhs[0] = userString;
return;
}
To test the above mexFunction after building to a MEX file, for example mexFunction.mexw32, invoke:
y = mexFunction