tags:

views:

134

answers:

1

I would like to pass a vector of strings from C++ to matlab. I have tried using the functions available such as mxCreateCharMatrixFromStrings but it doesn't give me the correct behavior.

So, I have something like this:

void mexFunction(
    int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[])
{
   vector<string> stringVector;
   stringVector.push_back("string 1");
   stringVector.push_back("string 2");
   //etc...

The problem is how do I get this vector to the matlab environment?

   plhs[0] = ???

My goal is to be able to run:

>> [strings] = MyFunc(...)
>> strings(1) = 'string 1'
+3  A: 

Storing a vector of strings as a char matrix requires that all of your strings are the same length and that they're stored contiguously in memory.

The best way to store an array of strings in MATLAB is with a cell array, try using mxCreateCellArray, mxSetCell, and mxGetCell. Under the hood, cell arrays are basically an array of pointers to other objects, char arrays, matrices, other cell arrays, etc..

gtownescapee
that worked...so far. thanks.
aduric

related questions