How do I copy data to a new string variable in a C-MEX file?

2 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
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

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!