This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Create 2-D Cell Array

This example shows how to create a cell array in a MEX file, using the mxcreatecellmatrix.c function, which places input arguments in a cell array.

C Code Analysis

To see the code, open mxcreatecellmatrix.c in the MATLAB® Editor.

Create a cell array for the number of input arguments.

cell_array_ptr = mxCreateCellMatrix((mwSize)nrhs,1);

Copy the input arguments into the cell array.

for( i=0; i<(mwIndex)nrhs; i++){
    mxSetCell(cell_array_ptr,i,mxDuplicateArray(prhs[i]));

Build and Test Example

Run the following commands from the MATLAB command line.

Build the example.

mex -v mxcreatecellmatrix.c

Create input arguments.

str1 = 'hello';
str2 = 'world';
num = 2012;

Create a 3-x-1 cell array and call disp to display the contents.

mxcreatecellmatrix(str1,str2,num)
The contents of the created cell is:

    'hello'
    'world'
    [2012]

Related Topics