MATLAB: Need helping using mxCreateCharArray

from

Hello, Below is my code that attempts to pass a char array to MATLAB from C++ but can't. Can some one please help.
#include <yvals.h>
#if (_MSC_VER >= 1600)
#define __STDC_UTF_16__
#endif
#include "mex.h"
#include <stdlib.h>
#include <vector>
#include "engine.h"
#include <iostream>
#include <windows.h>
#include <ctime>
#include <string>
using namespace std;
int main()
{
Engine *ep;
if (!(ep = engOpen(NULL)))
{
cout << "Can't open Matlab\n\n" << endl;
return -1;
}
char test[3][3] = {"11","12","13"};
int dims[2]={3,10};
mxArray *in = mxCreateCharArray(2, (const mwSize *)dims);
memcpy(mxGetPr(in), test, 3*sizeof(char));
engPutVariable(ep, "test", in); // Place the variable NS into the MATLAB workspace
}

Best Answer

Casting the int * to a mwSize * can crash tremendously for a 64 bit system. Better define dims as mwSize directly.
The char type in Matlab is actaully a uint16_t. Therefore the memcpy create rubbish.
mxGetPr is valid for double arrays only. This might work for some compilers, but it is not secure. Better use:
(mxChar *) mxGetData(in)
Anyhow, there is a specific MEX command for this task:
in = mxCreateCharMatrixFromStrings(mwSize(3), test);