Using the C Math Library    

Creating Cell Arrays

The MATLAB C Math Library allows you to create cell arrays by:

Using the Cell Array Creation Routine

You can create an array of empty cells using the mlfCell() routine. The following code fragment creates a 2-by-3-by-2 array of empty cells.

This code produces the following output.

MATLAB uses brackets to indicate cell array elements and [] represents an empty cell. You can then assign values to cells in the array using assignment. For an example of assigning a value to a cell in a cell array, see Using Assignment to Create Cell Arrays.

Using Cell Array Conversion Routines

You can also create cell arrays by converting other MATLAB arrays into cell arrays. The MATLAB C Math Library includes routines that convert a numeric array into a cell array, mlfNum2cell(), or a structure into a cell array, mlfStruct2cell().

The following code fragment creates a numeric array, using mlfOnes(), and converts it into a cell array using the mlfNum2cell() routine.

In this output, the brackets indicate that each element in the numeric array has been placed into a cell in the cell array.

The brackets indicate cell array elements.

Using Concatenation to Create Cell Arrays

You can group existing MATLAB arrays into a cell array by concatenation. In MATLAB, you use the {} (braces) operator to create cell arrays through concatenation. For example, you can use the following syntax in MATLAB to concatenate arrays into a cell array:

To create the same cell array through concatenation in a C program, use the MATLAB C Math Library mlfCellhcat() routine. This routine performs the same function as {}, the MATLAB cell concatenation operator.

To see the output from this code fragment, see Displaying the Contents of a Cell Array.

Using Assignment to Create Cell Arrays

You can also create a cell array by assigning a value to a location in a cell array, using the mlfIndexAssign() routine. The MATLAB C Math Library creates a cell array large enough to accommodate the specified location or expands an existing array. For more information about using indexing with cell arrays, see Chapter 5.

The following example is equivalent to the MATLAB statement,
A(2,2) = {17}. Note the use of curly braces in the index subscript format string: "{?,?}". This syntax indicates you want to create a cell array. Also note that the index subscript format string may be passed as a standard C character string; it does not need to be a MATLAB character array.

The following output shows the cell array created by this code fragment.


 Cell Arrays Displaying the Contents of a Cell Array