Using the C Math Library | ![]() ![]() |
Displaying the Contents of a Cell Array
When you use mlfPrintMatrix()
or mlfDisp()
to display a cell array, the MATLAB C Math Library displays the type of array stored in each cell but it does not display the contents of the cell (except for string arrays and scalar values). To view the contents of each cell, you must use the mlfCelldisp()
routine.
The mlfCelldisp()
routine supports a second, optional argument which specifies the text string used to identify each cell in the output. In the example, the character "D"
is passed to mlfCelldisp()
as its second argument. This appears in the output in the line that prefixes each cell, such as "D{1} ="
. If you do not specify this second argument, mlfCelldisp()
uses the text string "ans"
, as in "ans{1} ="
.
The following code fragment creates a cell array and prints out the cell array using both mlfPrintMatrix()
and mlfCelldisp()
.
mxArray *A = NULL; mxArray *B = NULL; mxArray *C = NULL; mxArray *D = NULL; static double data[] = { 1, 4, 2, 5, 3, 6 }; mlfAssign(&A, mlfColon(mlfScalar(1),mlfScalar(10),NULL)); mlfAssign(&B, mxCreateString("my string\n")); mlfAssign(&C, mlfDoubleMatrix(2, 3, data, NULL)); mlfAssign(&D, mlfCellhcat(A,B,C,NULL)); mlfPrintMatrix(mxCreateString("The mlfPrintMatrix() output:")); mlfPrintMatrix(D); mlfPrintMatrix(mxCreateString("The mlfCelldisp() output:")); mlfCelldisp(D,mxCreateString("D")); mxDestroyArray(A); mxDestroyArray(B); mxDestroyArray(C); mxDestroyArray(D);
This code produces the following output:
The mlfPrintMatrix() output: [2x3 double] [1x5 double] 'my string' The mlfCellDisp() output: D{1} = 1 2 3 4 5 6 7 8 9 10 D{2} = my string D{3} = 1 2 3 4 5 6
![]() | Creating Cell Arrays | MATLAB Structures | ![]() |