Spec-Zone.ru › Octave 9

Далее: Массивы ячеек с Mex-файлами, Предыдущее: Работа с матрицами и массивами в Mex-файлах, Вверх: Mex-файлы [Содержание][Индекс]

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–2023 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/v9.2.0/Character-Strings-in-Mex_002dFiles.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API