Cell Fun
Cell Fun
Cell Fun
Syntax
[A1,...,Am] = cellfun(func,C1,...,Cn)
[A1,...,Am] = cellfun(func,C1,...,Cn,Name,Value)
Description
[A1,...,Am] = cellfun(func,C1,...,Cn) calls the function specified by function
handle func and passes elements from cell arrays C1,...,Cn, where n is the number of inputs to
function func. Output arrays A1,...,Am, where m is the number of outputs from function func,
contain the combined outputs from the function calls. The ith iteration corresponds to the
syntax [A1(i),...,Am(i)] = func(C1{i},...,Cn{i}). The cellfun function does not perform
the calls to function func in a specific order.
[A1,...,Am] = cellfun(func,C1,...,Cn,Name,Value) calls function func with additional
options specified by one or more Name,Value pair arguments. Possible values
for Nameare 'UniformOutput' or 'ErrorHandler'.
Input Arguments
func Handle to a function that accepts n input arguments and returns m output arguments.
If function func corresponds to more than one function file (that is, if func represents a set of overloaded functions),
of the input arguments.
Backward Compatibility
C1,...,Cn Cell arrays that contain the n inputs required for function func. Each cell array must have the same dimensions.
Name-Value Pair Arguments
Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name
and Value is the corresponding value. Name must appear inside single quotes (' '). You can
specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.
Default: true
'ErrorHandler' Handle to a function that catches any errors that occur when MATLAB attempts to execute function func. Define this
function func.
MATLAB calls the specified error-handling function with two input arguments:
A structure with these fields:
identifier Error identifier.
Output Arguments
A1,...,Am Arrays that collect the m outputs from function func. Each array A is the same size as each of the inputs C1,...,Cn
Function func can return output arguments of different classes. However, if UniformOutput is true (the default)
The individual outputs from function func must be scalar values (numeric, logical, or character), scalar structures, or
The class of a particular output argument must be the same for each set of inputs. The class of the corresponding outpu
Examples
Compute the mean of each vector in cell array C.
C = {1:10, [2; 4; 6], []};
averages = cellfun(@mean, C)
Create a cell array that contains character vectors, and abbreviate each of them to their first three
characters. Because the output character vectors are nonscalar, set UniformOutput to false.
days = {'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'};
Compute the covariance between arrays in two cell arrays C and D. Because the covariance output
is nonscalar, set UniformOutput to false.
c1 = rand(5,1); c2 = rand(10,1); c3 = rand(15,1);
d1 = rand(5,1); d2 = rand(10,1); d3 = rand(15,1);
C = {c1, c2, c3};
D = {d1, d2, d3};
A = {rand(3)};
B = {rand(5)};
AgtB = cellfun(@(x,y) x > y, A, B, 'ErrorHandler', @errorfun, ...
'UniformOutput', false)
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
See Also
arrayfun | cell2mat | spfun | splitapply | structfun
Topics
Anonymous Functions
Create Function Handle
Introduced before R2006a
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('size',C,k) returns the size along the k-th dimension of each element
of C.
Limitations
If the cell array contains objects, cellfun does not call overloaded versions of the
function fname.
Example
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);
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
See Also
celldisp cellplot