如何在matlab中创建一个字符串数组?
问题描述:
我想从C++传递一个字符串向量到matlab。我曾尝试使用可用的功能,例如mxCreateCharMatrixFromStrings但它没有给我正确的行为。如何在matlab中创建一个字符串数组?
所以,我有这样的事情:
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...
的问题是如何得到这个向量的MATLAB环境?
plhs[0] = ???
我的目标是能够运行:
>> [strings] = MyFunc(...)
>> strings(1) = 'string 1'
答
将字符串存储为一个char矩阵的矢量要求您的所有字符串的长度相同,并且他们在连续存储记忆。
在MATLAB中存储字符串数组的最佳方法是使用单元格数组,尝试使用mxCreateCellArray,mxSetCell和mxGetCell。在引擎盖下,单元阵列基本上是指向其他对象,字符数组,矩阵,其他单元阵列等的指针数组。
工作到目前为止。谢谢。 – aduric 2010-05-19 17:27:58