Saturday, July 25, 2009

cell

Create cell array

Syntax

  • c = cell(n)
    c = cell(m,n) or c = cell([m n])
    c = cell(m,n,p,...) or c = cell([m n p ...])
    c = cell(size(A))
    c = cell(javaobj)

Description

c = cell(n) creates an n-by-n cell array of empty matrices. An error message appears if n is not a scalar.

c = cell(m,n) or c = cell([m,n]) creates an m-by-n cell array of empty matrices. Arguments m and n must be scalars.

c = cell(m,n,p,...) or c = cell([m n p ...]) creates an m-by-n-by-p-... cell array of empty matrices. Arguments m, n, p,... must be scalars.

c = cell(size(A)) creates a cell array the same size as A containing all empty matrices.

c = cell(javaobj) converts a Java array or Java object, javaobj, into a MATLAB cell array. Elements of the resulting cell array will be of the MATLAB type (if any) closest to the Java array elements or Java object.

Examples

This example creates a cell array that is the same size as another array, A.

  • A = ones(2,2)

    A =
    1 1
    1 1

    c = cell(size(A))

    c =
    [] []
    [] []

The next example converts an array of java.lang.String objects into a MATLAB cell array.

  • strArray = java_array('java.lang.String',3);
    strArray(1) = java.lang.String('one');
    strArray(2) = java.lang.String('two');
    strArray(3) = java.lang.String('three');

    cellArray = cell(strArray)
    cellArray =
    'one'
    'two'
    'three'
cell2mat

Convert cell array of matrices into single matrix

Syntax

  • m = cell2mat(c)

Description

m = cell2mat(c) converts a multidimensional cell array, c, with contents of the same data type into a single matrix, m. The contents of c must be able to concatenate into a hyperrectangle. Moreover, for each pair of neighboring cells, the dimensions of the cell's contents must match, excluding the dimension in which the cells are neighbors.

The example shown below combines matrices in a 3-by-2 cell array into a single 60-by-50 matrix:

  • cell2mat(c)



Remarks

The dimensionality (or number of dimensions) of m will match the highest dimensionality contained in the cell array.

cell2mat is not supported for cell arrays containing cell arrays or objects.

Examples

Combine the matrices in four cells of cell array C into the single matrix, M:

  • C = {[1] [2 3 4]; [5; 9] [6 7 8; 10 11 12]}
    C =
    [ 1] [1x3 double]
    [2x1 double] [2x3 double]

    C{1,1} C{1,2}
    ans = ans =
    1 2 3 4

    C{2,1} C{2,2}
    ans = ans =
    5 6 7 8
    9 10 11 12

    M = cell2mat(C)
    M =
    1 2 3 4
    5 6 7 8
    9 10 11 12
cell2struct

Convert cell array to structure array

Syntax

  • s = cell2struct(c,fields,dim)

Description

s = cell2struct(c,fields,dim) creates a structure array, s, from the information contained within cell array, c.

The fields argument specifies field names for the structure array. fields can be a character array or a cell array of strings.

The dim argument controls which axis of the cell array is to be used in creating the structure array. The length of c along the specified dimension must match the number of fields named in fields. In other words, the following must be true.

  • size(c,dim) == length(fields)       % if fields is a cell array
    size(c,dim) == size(fields,1) % if fields is a char array

Examples

The cell array, c, in this example contains information on trees. The three columns of the array indicate the common name, genus, and average height of a tree.

  • c = {'birch','betula',65;  'maple','acer',50}
    c =
    'birch' 'betula' [65]
    'maple' 'acer' [50]

To put this information into a structure with the fields name, genus, and height, use cell2struct along the second dimension of the 2-by-3 cell array.

  • fields = {'name', 'genus', 'height'};
    s = cell2struct(c, fields, 2);

This yields the following 2-by-1 structure array.

  • s(1)                        s(2)
    ans = ans =
    name: 'birch' name: 'maple'
    genus: 'betula' genus: 'acer'
    height: 65 height: 50
celldisp

Display cell array contents.

Syntax

  • celldisp(C)
    celldisp(C,name)

Description

celldisp(C) recursively displays the contents of a cell array.

celldisp(C,name) uses the string name for the display instead of the name of the first input (or ans).

Example

Use celldisp to display the contents of a 2-by-3 cell array:

  • C = {[1 2] 'Tony' 3+4i; [1 2;3 4] -5 'abc'};
    celldisp(C)

    C{1,1} =
    1 2

    C{2,1} =
    1 2
    3 4

    C{1,2} =
    Tony

    C{2,2} =
    -5

    C{1,3} =
    3.0000+ 4.0000i

    C{2,3} =
    abc
cellfun

Apply a function to each element in a cell array

Syntax

  • D = cellfun('fname',C)
    D = cellfun('size',C,k)
    D = cellfun('isclass',C,classname)

Description

D = cellfun('fname',C) applies the function fname to the elements of the cell array C and returns the results in the double array D. Each element of D contains the value returned by fname for the corresponding element in C. The output array D is the same size as the cell array C.

These functions are supported:


Function
Return Value
isempty
true for an empty cell element
islogical
true for a logical cell element
isreal
true for a real cell element
length
Length of the cell element
ndims
Number of dimensions of the cell element
prodofsize
Number of elements in the cell element

D = cellfun('size',C,k) returns the size along the k-th dimension of each element of C.

D = cellfun('isclass',C,'classname') returns true for each element of C that matches classname. This function syntax returns false for objects that are a subclass of classname.

Limitations

If the cell array contains objects, cellfun does not call overloaded versions of the function fname.

Example

Consider this 2-by-3 cell array:

  • C{1,1} = [1 2; 4 5];
    C{1,2} = 'Name';
    C{1,3} = pi;
    C{2,1} = 2 + 4i;
    C{2,2} = 7;
    C{2,3} = magic(3);

cellfun returns a 2-by-3 double array:

  • D = cellfun('isreal',C)

    D =
    1 1 1
    0 1 1

    len = cellfun('length',C)

    len =
    2 4 1
    1 1 3

    isdbl = cellfun('isclass',C,'double')

    isdbl =
    1 0 1
    1 1 1
cellplot

Graphically display the structure of cell arrays

Syntax

  • cellplot(c)
    cellplot(c,'legend')
    handles = cellplot(...)

Description

cellplot(c) displays a figure window that graphically represents the contents of c. Filled rectangles represent elements of vectors and arrays, while scalars and short text strings are displayed as text.

cellplot(c,'legend') also puts a legend next to the plot.

handles = cellplot(c) displays a figure window and returns a vector of surface handles.

Limitations

The cellplot function can display only two-dimensional cell arrays.

Examples

Consider a 2-by-2 cell array containing a matrix, a vector, and two text strings:

  • c{1,1} = '2-by-2';
    c{1,2} = 'eigenvalues of eye(2)';
    c{2,1} = eye(2);
    c{2,2} = eig(eye(2));

The command cellplot(c) produces:



cellstr

Create cell array of strings from character array

Syntax

  • c = cellstr(S)

Description

c = cellstr(S) places each row of the character array S into separate cells of c. Use the char function to convert back to a string matrix.

Examples

Given the string matrix

  • S=['abc ';'defg';'hi  ']

    S =
    abc
    defg
    hi

    whos S
    Name Size Bytes Class
    S 3x4 24 char array

The following command returns a 3-by-1 cell array.

  • c = cellstr(S)

    c =
    'abc'
    'defg'
    'hi'

    whos c
    Name Size Bytes Class
    c 3x1 294 cell array

No comments:

Post a Comment