我想将一个字符串 vector 从 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...
问题是如何将这个 vector 导入到matlab环境中?
plhs[0] = ???
我的目标是能够运行:
>> [strings] = MyFunc(...)
>> strings(1) = 'string 1'
最佳答案
将字符串 vector 存储为 char 矩阵需要所有字符串的长度相同,并且它们连续存储在内存中。
在 MATLAB 中存储字符串数组的最佳方法是使用元胞数组,尝试使用 mxCreateCellArray
、mxSetCell
和 mxGetCell
。在幕后,元胞数组基本上是指向其他对象、字符数组、矩阵、其他元胞数组等的指针数组。
关于c++ - 如何在 MATLAB 中创建字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2867340/