如何在matlab中创建一个字符串数组?

adu*_*ric 3 c++ matlab mex

我想将一个字符串向量从C++传递给matlab.我已经尝试使用可用的函数,如mxCreateCharMatrixFromStrings,但它没有给我正确的行为.

所以,我有这样的事情:

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...
Run Code Online (Sandbox Code Playgroud)

问题是我如何将这个向量带到matlab环境中?

   plhs[0] = ???
Run Code Online (Sandbox Code Playgroud)

我的目标是能够运行:

>> [strings] = MyFunc(...)
>> strings(1) = 'string 1'
Run Code Online (Sandbox Code Playgroud)

小智 5

将字符串向量存储为char矩阵要求所有字符串都具有相同的长度,并且它们将连续存储在内存中.

在MATLAB中存储字符串数组的最佳方法是使用单元数组,尝试使用mxCreateCellArray,mxSetCell和mxGetCell.在引擎盖下,单元格数组基本上是指向其他对象,字符数组,矩阵,其他单元格数组等的指针数组.