登录论坛

查看完整版本 : 如何在MATLAB中创建字符串数组?


poster
2019-12-10, 20:41
我想将字符串向量从C ++传递给MATLAB。我尝试使用mxCreateCharMatrixFromStrings可用函数,但是它没有给我正确的行为。

所以,我有这样的事情:

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { vector stringVector; stringVector.push_back("string 1"); stringVector.push_back("string 2"); //etc... 问题是如何将这个向量传递到matlab环境?

plhs[0] = ??? 我的目标是能够运行:

>> [strings] = MyFunc(...) >> strings(1) = 'string 1'

回答:

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

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



更多&回答... (https://stackoverflow.com/questions/2867340)