A.2.3 Строки символов в Mex-файлах
Поскольку mex-файлы не различают одинарные и двойные кавычки, как Octave, использование строк и матриц символов, возможно, менее сложно. Пример их использования, аналогичный демонстрации в stringdemo.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 для матрицы в любом случае идут подряд.
© 1996–2022 The Octave Project Developers
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://docs.octave.org/v7.2.0/Character-Strings-in-Mex_002dFiles.html