<html lang="en"> <head> <title>Processing Data in Cell Arrays - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Cell-Arrays.html#Cell-Arrays" title="Cell Arrays"> <link rel="prev" href="Cell-Arrays-of-Strings.html#Cell-Arrays-of-Strings" title="Cell Arrays of Strings"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Processing-Data-in-Cell-Arrays"></a> <p> Previous: <a rel="previous" accesskey="p" href="Cell-Arrays-of-Strings.html#Cell-Arrays-of-Strings">Cell Arrays of Strings</a>, Up: <a rel="up" accesskey="u" href="Cell-Arrays.html#Cell-Arrays">Cell Arrays</a> <hr> </div> <h4 class="subsection">6.2.5 Processing Data in Cell Arrays</h4> <p>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 <code>for</code> loops. The same idea can be implemented more easily through the use of the <code>cellfun</code> function that calls a user-specified function on all elements of a cell array. <!-- ./DLD-FUNCTIONS/cellfun.cc --> <p><a name="doc_002dcellfun"></a> <div class="defun"> — Loadable Function: <b>cellfun</b> (<var>name, c</var>)<var><a name="index-cellfun-400"></a></var><br> — Loadable Function: <b>cellfun</b> (<var>"size", c, k</var>)<var><a name="index-cellfun-401"></a></var><br> — Loadable Function: <b>cellfun</b> (<var>"isclass", c, class</var>)<var><a name="index-cellfun-402"></a></var><br> — Loadable Function: <b>cellfun</b> (<var>func, c</var>)<var><a name="index-cellfun-403"></a></var><br> — Loadable Function: <b>cellfun</b> (<var>func, c, d</var>)<var><a name="index-cellfun-404"></a></var><br> — Loadable Function: [<var>a</var>, <var>b</var>] = <b>cellfun</b> (<var><small class="dots">...</small></var>)<var><a name="index-cellfun-405"></a></var><br> — Loadable Function: <b>cellfun</b> (<var><small class="dots">...</small>, 'ErrorHandler', errfunc</var>)<var><a name="index-cellfun-406"></a></var><br> — Loadable Function: <b>cellfun</b> (<var><small class="dots">...</small>, 'UniformOutput', val</var>)<var><a name="index-cellfun-407"></a></var><br> <blockquote> <p>Evaluate the function named <var>name</var> on the elements of the cell array <var>c</var>. Elements in <var>c</var> are passed on to the named function individually. The function <var>name</var> can be one of the functions <dl> <dt><code>isempty</code><dd>Return 1 for empty elements. <br><dt><code>islogical</code><dd>Return 1 for logical elements. <br><dt><code>isreal</code><dd>Return 1 for real elements. <br><dt><code>length</code><dd>Return a vector of the lengths of cell elements. <br><dt><code>ndims</code><dd>Return the number of dimensions of each element. <br><dt><code>prodofsize</code><dd>Return the product of dimensions of each element. <br><dt><code>size</code><dd>Return the size along the <var>k</var>-th dimension. <br><dt><code>isclass</code><dd>Return 1 for elements of <var>class</var>. </dl> <p>Additionally, <code>cellfun</code> accepts an arbitrary function <var>func</var> 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 <var>x</var>, and it must return a string value. The function can take one or more arguments, with the inputs args given by <var>c</var>, <var>d</var>, etc. Equally the function can return one or more output arguments. For example <pre class="example"> cellfun (@atan2, {1, 0}, {0, 1}) ⇒ans = [1.57080 0.00000] </pre> <p>Note that the default output argument is an array of the same size as the input arguments. <p>If the parameter 'UniformOutput' is set to true (the default), then the function must return a single element which will be concatenated into the return value. If 'UniformOutput' is false, the outputs are concatenated in a cell array. For example <pre class="example"> cellfun ("tolower(x)", {"Foo", "Bar", "FooBar"}, "UniformOutput",false) ⇒ ans = {"foo", "bar", "foobar"} </pre> <p>Given the parameter 'ErrorHandler', then <var>errfunc</var> defines a function to call in case <var>func</var> generates an error. The form of the function is <pre class="example"> function [...] = errfunc (<var>s</var>, ...) </pre> <p>where there is an additional input argument to <var>errfunc</var> relative to <var>func</var>, given by <var>s</var>. 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 <pre class="example"> function y = foo (s, x), y = NaN; endfunction cellfun (@factorial, {-1,2},'ErrorHandler',@foo) ⇒ ans = [NaN 2] </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002disempty.html#doc_002disempty">isempty</a>, <a href="doc_002dislogical.html#doc_002dislogical">islogical</a>, <a href="doc_002disreal.html#doc_002disreal">isreal</a>, <a href="doc_002dlength.html#doc_002dlength">length</a>, <a href="doc_002dndims.html#doc_002dndims">ndims</a>, <a href="doc_002dnumel.html#doc_002dnumel">numel</a>, <a href="doc_002dsize.html#doc_002dsize">size</a>. </p></blockquote></div> <p>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 <code>cell2mat</code> and <code>cell2struct</code> functions. <!-- ./general/cell2mat.m --> <p><a name="doc_002dcell2mat"></a> <div class="defun"> — Function File: <var>m</var> = <b>cell2mat</b> (<var>c</var>)<var><a name="index-cell2mat-408"></a></var><br> <blockquote><p>Convert the cell array <var>c</var> into a matrix by concatenating all elements of <var>c</var> into a hyperrectangle. Elements of <var>c</var> must be numeric, logical or char, and <code>cat</code> must be able to concatenate them together. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmat2cell.html#doc_002dmat2cell">mat2cell</a>, <a href="doc_002dnum2cell.html#doc_002dnum2cell">num2cell</a>. </p></blockquote></div> <!-- ov-struct.cc --> <p><a name="doc_002dcell2struct"></a> <div class="defun"> — Built-in Function: <b>cell2struct</b> (<var>cell, fields, dim</var>)<var><a name="index-cell2struct-409"></a></var><br> <blockquote><p>Convert <var>cell</var> to a structure. The number of fields in <var>fields</var> must match the number of elements in <var>cell</var> along dimension <var>dim</var>, that is <code>numel (</code><var>fields</var><code>) == size (</code><var>cell</var><code>, </code><var>dim</var><code>)</code>. <pre class="example"> A = cell2struct ({'Peter', 'Hannah', 'Robert'; 185, 170, 168}, {'Name','Height'}, 1); A(1) ⇒ ans = { Height = 185 Name = Peter } </pre> </blockquote></div> </body></html>