从于mex文件不像Octave那样区分单引号字符串和双引号字符串,所以字符串和字符矩阵的使用可能不那么复杂。它们的使用示例与中的mo相似字符串emo.cc在文件中给出mystring.c,如下图所示。
#include <string.h>
#include "mex.h"
void
mexFunction (int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
mwSize m, n;
mwIndex i, j;
mxChar *pi, *po;
if (nrhs != 1 || ! mxIsChar (prhs[0])
|| mxGetNumberOfDimensions (prhs[0]) > 2)
mexErrMsgTxt ("ARG1 must be a char matrix");
m = mxGetM (prhs[0]);
n = mxGetN (prhs[0]);
pi = mxGetChars (prhs[0]);
plhs[0] = mxCreateNumericMatrix (m, n, mxCHAR_CLASS, mxREAL);
po = mxGetChars (plhs[0]);
for (j = 0; j < n; j++)
for (i = 0; i < m; i++)
po[j*m + m - 1 - i] = pi[j*m + i];
}
其预期输出的一个例子是
mystring (["First String"; "Second String"]) ⇒ Second String First String
mex接口中用于处理字符串的其他函数有mxCreateString, mxArrayToString和mxCreateCharMatrixFromStrings。在mex文件中,字符串被认为是向量而不是矩阵。这可能是一种任意的区别,因为mxArray因为矩阵在任何情况下都是连续的。
版权所有 © 2024-2025 Octave中文网
ICP备案/许可证号:黑ICP备2024030411号-2