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 the fileopen the file in 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]
Was this topic helpful?