3
<title>Processing Data in Cell Arrays - Untitled</title>
4
<meta http-equiv="Content-Type" content="text/html">
5
<meta name="description" content="Untitled">
6
<meta name="generator" content="makeinfo 4.11">
7
<link title="Top" rel="start" href="index.html#Top">
8
<link rel="up" href="Cell-Arrays.html#Cell-Arrays" title="Cell Arrays">
9
<link rel="prev" href="Cell-Arrays-of-Strings.html#Cell-Arrays-of-Strings" title="Cell Arrays of Strings">
10
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
11
<meta http-equiv="Content-Style-Type" content="text/css">
12
<style type="text/css"><!--
13
pre.display { font-family:inherit }
14
pre.format { font-family:inherit }
15
pre.smalldisplay { font-family:inherit; font-size:smaller }
16
pre.smallformat { font-family:inherit; font-size:smaller }
17
pre.smallexample { font-size:smaller }
18
pre.smalllisp { font-size:smaller }
19
span.sc { font-variant:small-caps }
20
span.roman { font-family:serif; font-weight:normal; }
21
span.sansserif { font-family:sans-serif; font-weight:normal; }
27
<a name="Processing-Data-in-Cell-Arrays"></a>
28
Previous: <a rel="previous" accesskey="p" href="Cell-Arrays-of-Strings.html#Cell-Arrays-of-Strings">Cell Arrays of Strings</a>,
29
Up: <a rel="up" accesskey="u" href="Cell-Arrays.html#Cell-Arrays">Cell Arrays</a>
33
<h4 class="subsection">6.2.4 Processing Data in Cell Arrays</h4>
35
<p>Data that is stored in a cell array can be processed in several ways
36
depending on the actual data. The most simple way to process that data
37
is to iterate through it using one or more <code>for</code> loops. The same
38
idea can be implemented easier through the use of the <code>cellfun</code>
39
function that calls a user specified function on all elements of a cell
42
<p><a name="doc_002dcellfun"></a>
45
— Loadable Function: <b>cellfun</b> (<var>name, c</var>)<var><a name="index-cellfun-348"></a></var><br>
46
— Loadable Function: <b>cellfun</b> (<var>"size", c, k</var>)<var><a name="index-cellfun-349"></a></var><br>
47
— Loadable Function: <b>cellfun</b> (<var>"isclass", c, class</var>)<var><a name="index-cellfun-350"></a></var><br>
48
— Loadable Function: <b>cellfun</b> (<var>func, c</var>)<var><a name="index-cellfun-351"></a></var><br>
49
— Loadable Function: <b>cellfun</b> (<var>func, c, d</var>)<var><a name="index-cellfun-352"></a></var><br>
50
— Loadable Function: [<var>a</var>, <var>b</var>] = <b>cellfun</b> (<var><small class="dots">...</small></var>)<var><a name="index-cellfun-353"></a></var><br>
51
— Loadable Function: <b>cellfun</b> (<var><small class="dots">...</small>, 'ErrorHandler', errfunc</var>)<var><a name="index-cellfun-354"></a></var><br>
52
— Loadable Function: <b>cellfun</b> (<var><small class="dots">...</small>, 'UniformOutput', val</var>)<var><a name="index-cellfun-355"></a></var><br>
54
<p>Evaluate the function named <var>name</var> on the elements of the cell array
55
<var>c</var>. Elements in <var>c</var> are passed on to the named function
56
individually. The function <var>name</var> can be one of the functions
59
<dt><code>isempty</code><dd>Return 1 for empty elements.
60
<br><dt><code>islogical</code><dd>Return 1 for logical elements.
61
<br><dt><code>isreal</code><dd>Return 1 for real elements.
62
<br><dt><code>length</code><dd>Return a vector of the lengths of cell elements.
63
<br><dt><code>ndims</code><dd>Return the number of dimensions of each element.
64
<br><dt><code>prodofsize</code><dd>Return the product of dimensions of each element.
65
<br><dt><code>size</code><dd>Return the size along the <var>k</var>-th dimension.
66
<br><dt><code>isclass</code><dd>Return 1 for elements of <var>class</var>.
69
<p>Additionally, <code>cellfun</code> accepts an arbitrary function <var>func</var>
70
in the form of an inline function, function handle, or the name of a
71
function (in a character string). In the case of a character string
72
argument, the function must accept a single argument named <var>x</var>, and
73
it must return a string value. The function can take one or more arguments,
74
with the inputs args given by <var>c</var>, <var>d</var>, etc. Equally the function
75
can return one or more output arguments. For example
77
<pre class="example"> cellfun (@atan2, {1, 0}, {0, 1})
78
ans = [1.57080 0.00000]
80
<p>Note that the default output argument is an array of the same size as the
83
<p>If the param 'UniformOutput' is set to true (the default), then the function
84
must return either a single element which will be concatenated into the
85
return value. If 'UniformOutput is false, the outputs are concatenated in
86
a cell array. For example
88
<pre class="example"> cellfun ("tolower(x)", {"Foo", "Bar", "FooBar"},
89
"UniformOutput",false)
90
ans = {"foo", "bar", "foobar"}
92
<p>Given the parameter 'ErrorHandler', then <var>errfunc</var> defines a function to
93
call in case <var>func</var> generates an error. The form of the function is
95
<pre class="example"> function [...] = errfunc (<var>s</var>, ...)
97
<p>where there is an additional input argument to <var>errfunc</var> relative to
98
<var>func</var>, given by <var>s</var>. This is a structure with the elements
99
'identifier', 'message' and 'index', giving respectively the error
100
identifier, the error message, and the index into the input arguments
101
of the element that caused the error. For example
103
<pre class="example"> function y = foo (s, x), y = NaN; endfunction
104
cellfun (@factorial, {-1,2},'ErrorHandler',@foo)
110
<strong>See also:</strong> isempty, islogical, isreal, length, ndims, numel, size, isclass.
111
</p></blockquote></div>
113
<p>An alternative is to convert the data to a different container, such as
114
a matrix or a data structure. Depending on the data this is possible
115
using the <code>cell2mat</code> and <code>cell2struct</code> functions.
117
<p><a name="doc_002dcell2mat"></a>
120
— Function File: <var>m</var> = <b>cell2mat</b> (<var>c</var>)<var><a name="index-cell2mat-356"></a></var><br>
121
<blockquote><p>Convert the cell array <var>c</var> into a matrix by concatenating all
122
elements of <var>c</var> into a hyperrectangle. Elements of <var>c</var> must
123
be numeric, logical or char, and <code>cat</code> must be able to
124
concatenate them together.
128
<strong>See also:</strong> mat2cell, num2cell.
129
</p></blockquote></div>
131
<p><a name="doc_002dcell2struct"></a>
134
— Built-in Function: <b>cell2struct</b> (<var>cell, fields, dim</var>)<var><a name="index-cell2struct-357"></a></var><br>
135
<blockquote><p>Convert <var>cell</var> to a structure. The number of fields in <var>fields</var>
136
must match the number of elements in <var>cell</var> along dimension <var>dim</var>,
137
that is <code>numel (</code><var>fields</var><code>) == size (</code><var>cell</var><code>, </code><var>dim</var><code>)</code>.
139
<pre class="example"> A = cell2struct ({'Peter', 'Hannah', 'Robert';
141
{'Name','Height'}, 1);