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(
creates a cell array the same size as size
(A)) 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
.
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'
Convert cell array of matrices into single matrix
Syntax
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:
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
Convert cell array to structure array
Syntax
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.
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.
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
Syntax
Description
celldisp(C)
recursively displays the contents of a cell array.
celldisp(C,
uses the string name
) 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
Apply a function to each element in a cell array
Syntax
Description
applies the function D = cellfun('fname',C)
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:
D = cellfun('
returns the size along the size
',C,k) k
-th dimension of each element of C
.
D = cellfun('isclass',C,'
returns classname'
) 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:
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
Graphically display the structure of cell arrays
Syntax
Description
displays a figure window that graphically represents the contents of cellplot(c)
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.
displays a figure window and returns a vector of surface handles.handles = cellplot(c)
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:
The command cellplot(c)
produces:
Create cell array of strings from character array
Syntax
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
No comments:
Post a Comment