<html lang="en"> <head> <title>Cell Arrays with Mex-Files - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <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="Character-Strings-in-Mex_002dFiles.html#Character-Strings-in-Mex_002dFiles" title="Character Strings in Mex-Files"> <link rel="next" href="Structures-with-Mex_002dFiles.html#Structures-with-Mex_002dFiles" title="Structures 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"> <a name="Cell-Arrays-with-Mex-Files"></a> <a name="Cell-Arrays-with-Mex_002dFiles"></a> <p> Next: <a rel="next" accesskey="n" href="Structures-with-Mex_002dFiles.html#Structures-with-Mex_002dFiles">Structures with Mex-Files</a>, Previous: <a rel="previous" accesskey="p" href="Character-Strings-in-Mex_002dFiles.html#Character-Strings-in-Mex_002dFiles">Character Strings 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.4 Cell Arrays with Mex-Files</h4> <p>We can perform exactly the same operations in Cell arrays in mex-files as we can in oct-files. An example that reduplicates the functional of the <samp><span class="file">celldemo.cc</span></samp> oct-file in a mex-file is given by <samp><span class="file">mycell.c</span></samp> as below <pre class="example"><pre class="verbatim"> #include "mex.h" void mexFunction (int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { mwSize n; mwIndex i; if (nrhs != 1 || ! mxIsCell (prhs[0])) mexErrMsgTxt ("expects cell"); n = mxGetNumberOfElements (prhs[0]); n = (n > nlhs ? nlhs : n); for (i = 0; i < n; i++) plhs[i] = mxDuplicateArray (mxGetCell (prhs[0], i)); } </pre> </pre> <p class="noindent">which as can be seen below has exactly the same behavior as the oct-file version. <pre class="example"> [b1, b2, b3] = mycell ({1, [1, 2], "test"}) ⇒ b1 = 1 b2 = 1 2 b3 = test </pre> <p>Note in the example the use of the <code>mxDuplicateArray</code> function. This is needed as the <code>mxArray</code> pointer returned by <code>mxGetCell</code> might be deallocated. The inverse function to <code>mxGetCell</code> is <code>mcSetCell</code> and is defined as <pre class="example"> void mxSetCell (mxArray *ptr, int idx, mxArray *val); </pre> <p>Finally, to create a cell array or matrix, the appropriate functions are <pre class="example"> mxArray *mxCreateCellArray (int ndims, const int *dims); mxArray *mxCreateCellMatrix (int m, int n); </pre> </body></html>