Next: Cell Arrays with Mex-Files, Previous: Working with Matrices and Arrays in Mex-Files, Up: Mex-Files
As mex-files do not make the distinction between single and double quoted strings within Octave, there is perhaps less complexity in the use of strings and character matrices in mex-files. An example of their use, that parallels the demo in stringdemo.cc, is given in the file mystring.c, as seen below.
#include <string.h>
#include "mex.h"
void
mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mwIndex i, j;
mwSize m, n;
mxChar *pi, *po;
if (nrhs != 1 || ! mxIsChar (prhs[0]) ||
mxGetNumberOfDimensions (prhs[0]) > 2)
mexErrMsgTxt ("expecting 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];
}
An example of its expected output is
mystring(["First String"; "Second String"])
=> s1 = Second String
First String
There are a couple of additional functions available in mex-files of
interest in the treatment of strings. These are mxCreateString,
mxArrayToString and mxCreateCharMatrixFromStrings. A
string in a mex-file is considered to be a vector rather than a
matrix. This is perhaps an arbitrary distinction as the data in the
mxArray for the matrix is consequetive in any case.