c++ - 如何在 MATLAB 中创建字符串数组?

标签 c++ matlab mex

我想将一个字符串 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 中存储字符串数组的最佳方法是使用元胞数组,尝试使用 mxCreateCellArraymxSetCellmxGetCell。在幕后,元胞数组基本上是指向其他对象、字符数组、矩阵、其他元胞数组等的指针数组。

关于c++ - 如何在 MATLAB 中创建字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2867340/

相关文章:

c++ - 读取 std::string 行(支持空格和特殊键)

matlab - 使用 MATLAB 的离散数学类(class)的项目想法?

python - 相当于 Python 中 Matlab 中的 'ismember' 吗?

c++ - 如何使用 Microsoft Visual C++ 2010 通过 MATLAB 7.10 (R2010a) 创建 MEX 文件?

c++ - MATLAB R2013b 不支持 Visual Studio 2013?

.net - 使用 WCF (https) 时 WSDL 超链接中的 URL 错误

c++ - do-while 循环不结束?

c++ - 在 C++ 中重用 strftime

matlab - matlab中向量与 block 矩阵相乘

c++ - winelib的优势?