1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
<html lang="en">
<head>
<title>Character Strings in Mex-Files - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Mex_002dFiles.html#Mex_002dFiles" title="Mex-Files">
<link rel="prev" href="Working-with-Matrices-and-Arrays-in-Mex_002dFiles.html#Working-with-Matrices-and-Arrays-in-Mex_002dFiles" title="Working with Matrices and Arrays in Mex-Files">
<link rel="next" href="Cell-Arrays-with-Mex_002dFiles.html#Cell-Arrays-with-Mex_002dFiles" title="Cell Arrays with Mex-Files">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Character-Strings-in-Mex-Files"></a>
<a name="Character-Strings-in-Mex_002dFiles"></a>
Next: <a rel="next" accesskey="n" href="Cell-Arrays-with-Mex_002dFiles.html#Cell-Arrays-with-Mex_002dFiles">Cell Arrays with Mex-Files</a>,
Previous: <a rel="previous" accesskey="p" href="Working-with-Matrices-and-Arrays-in-Mex_002dFiles.html#Working-with-Matrices-and-Arrays-in-Mex_002dFiles">Working with Matrices and Arrays in Mex-Files</a>,
Up: <a rel="up" accesskey="u" href="Mex_002dFiles.html#Mex_002dFiles">Mex-Files</a>
<hr>
</div>
<h4 class="subsection">A.2.3 Character Strings in Mex-Files</h4>
<p>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 <samp><span class="file">stringdemo.cc</span></samp>, is given in the
file <samp><span class="file">mystring.c</span></samp>, as seen below.
<pre class="example"><pre class="verbatim"> #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];
}
</pre></pre>
<p class="noindent">An example of its expected output is
<pre class="example"> mystring(["First String"; "Second String"])
s1 = Second String
First String
</pre>
<p>Other functions in the mex interface for handling character strings are
<code>mxCreateString</code>, <code>mxArrayToString</code>, and
<code>mxCreateCharMatrixFromStrings</code>. In a mex-file, a character string
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
consecutive in any case.
</body></html>
|