Processing Data in Cell Arrays - GNU Octave
Description: GNU Octave

Previous: Cell Arrays of Strings, Up: Cell Arrays

#### 6.2.5 Processing Data in Cell Arrays

Data that is stored in a cell array can be processed in several ways depending on the actual data. The simplest way to process that data is to iterate through it using one or more `for` loops. The same idea can be implemented more easily through the use of the `cellfun` function that calls a user-specified function on all elements of a cell array.

— Loadable Function: cellfun (name, C)
— Loadable Function: cellfun ("size", C, k)
— Loadable Function: cellfun ("isclass", C, class)
— Loadable Function: cellfun (func, C)
— Loadable Function: cellfun (func, C, D)
— Loadable Function: [a, ...] = cellfun (...)
— Loadable Function: cellfun (..., 'ErrorHandler', errfunc)
— Loadable Function: cellfun (..., 'UniformOutput', val)

> Evaluate the function named name on the elements of the cell array C. Elements in C are passed on to the named function individually. The function name can be one of the functions

> `isempty`
>   Return 1 for empty elements.
> `islogical`
>   Return 1 for logical elements.
> `isreal`
>   Return 1 for real elements.
> `length`
>   Return a vector of the lengths of cell elements.
> `ndims`
>   Return the number of dimensions of each element.
> `prodofsize`
>   Return the product of dimensions of each element.
> `size`
>   Return the size along the k-th dimension.
> `isclass`
>   Return 1 for elements of class.

> Additionally, `cellfun` accepts an arbitrary function func in the form of an inline function, function handle, or the name of a function (in a character string). In the case of a character string argument, the function must accept a single argument named x, and it must return a string value. The function can take one or more arguments, with the inputs arguments given by C, D, etc. Equally the function can return one or more output arguments. For example:

>               cellfun (@atan2, {1, 0}, {0, 1})
>                    ⇒ans = [1.57080   0.00000]

> The number of output arguments of `cellfun` matches the number of output arguments of the function. The outputs of the function will be collected into the output arguments of `cellfun` like this:

>               function [a, b] = twoouts (x)
>                 a = x;
>                 b = x*x;
>               endfunction
>               [aa, bb] = cellfun(@twoouts, {1, 2, 3})
>                    ⇒
>                       aa =
>                          1 2 3
>                       bb =
>                          1 4 9

> Note that per default the output argument(s) are arrays of the same size as the input arguments. Input arguments that are singleton (1x1) cells will be automatically expanded to the size of the other arguments.

> If the parameter 'UniformOutput' is set to true (the default), then the function must return scalars which will be concatenated into the return array(s). If 'UniformOutput' is false, the outputs are concatenated into a cell array (or cell arrays). For example:

>               cellfun ("tolower(x)", {"Foo", "Bar", "FooBar"},
>                        "UniformOutput",false)
>               ⇒ ans = {"foo", "bar", "foobar"}

> Given the parameter 'ErrorHandler', then errfunc defines a function to call in case func generates an error. The form of the function is

>               function [...] = errfunc (s, ...)

> where there is an additional input argument to errfunc relative to func, given by s. This is a structure with the elements 'identifier', 'message' and 'index', giving respectively the error identifier, the error message, and the index into the input arguments of the element that caused the error. For example:

>               function y = foo (s, x), y = NaN; endfunction
>               cellfun (@factorial, {-1,2},'ErrorHandler',@foo)
>               ⇒ ans = [NaN 2]

> See also: arrayfun, structfun, spfun.

An alternative is to convert the data to a different container, such as a matrix or a data structure. Depending on the data this is possible using the `cell2mat` and `cell2struct` functions.

— Function File: m = cell2mat (c)

> Convert the cell array c into a matrix by concatenating all elements of c into a hyperrectangle. Elements of c must be numeric, logical or char matrices, or cell arrays, and `cat` must be able to concatenate them together.

> See also: mat2cell, num2cell.

— Built-in Function: cell2struct (cell, fields, dim)

> Convert cell to a structure. The number of fields in fields must match the number of elements in cell along dimension dim, that is `numel (`fields`) == size (`cell`,` dim`)`.

>               A = cell2struct ({'Peter', 'Hannah', 'Robert';
>                                  185, 170, 168},
>                                {'Name','Height'}, 1);
>               A(1)
>                    ⇒ ans =
>                       {
>                         Name   = Peter
>                         Height = 185
>                       }