~ubuntu-branches/debian/sid/octave3.0/sid

« back to all changes in this revision

Viewing changes to doc/interpreter/HTML/Processing-Data-in-Cell-Arrays.html

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere
  • Date: 2007-12-23 16:04:15 UTC
  • Revision ID: james.westby@ubuntu.com-20071223160415-n4gk468dihy22e9v
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html lang="en">
 
2
<head>
 
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; } 
 
22
--></style>
 
23
</head>
 
24
<body>
 
25
<div class="node">
 
26
<p>
 
27
<a name="Processing-Data-in-Cell-Arrays"></a>
 
28
Previous:&nbsp;<a rel="previous" accesskey="p" href="Cell-Arrays-of-Strings.html#Cell-Arrays-of-Strings">Cell Arrays of Strings</a>,
 
29
Up:&nbsp;<a rel="up" accesskey="u" href="Cell-Arrays.html#Cell-Arrays">Cell Arrays</a>
 
30
<hr>
 
31
</div>
 
32
 
 
33
<h4 class="subsection">6.2.4 Processing Data in Cell Arrays</h4>
 
34
 
 
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
 
40
array.
 
41
 
 
42
   <p><a name="doc_002dcellfun"></a>
 
43
 
 
44
<div class="defun">
 
45
&mdash; Loadable Function:  <b>cellfun</b> (<var>name, c</var>)<var><a name="index-cellfun-348"></a></var><br>
 
46
&mdash; Loadable Function:  <b>cellfun</b> (<var>"size", c, k</var>)<var><a name="index-cellfun-349"></a></var><br>
 
47
&mdash; Loadable Function:  <b>cellfun</b> (<var>"isclass", c, class</var>)<var><a name="index-cellfun-350"></a></var><br>
 
48
&mdash; Loadable Function:  <b>cellfun</b> (<var>func, c</var>)<var><a name="index-cellfun-351"></a></var><br>
 
49
&mdash; Loadable Function:  <b>cellfun</b> (<var>func, c, d</var>)<var><a name="index-cellfun-352"></a></var><br>
 
50
&mdash; 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
&mdash; Loadable Function:  <b>cellfun</b> (<var><small class="dots">...</small>, 'ErrorHandler', errfunc</var>)<var><a name="index-cellfun-354"></a></var><br>
 
52
&mdash; Loadable Function:  <b>cellfun</b> (<var><small class="dots">...</small>, 'UniformOutput', val</var>)<var><a name="index-cellfun-355"></a></var><br>
 
53
<blockquote>
 
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
 
57
 
 
58
          <dl>
 
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>. 
 
67
</dl>
 
68
 
 
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
 
76
 
 
77
     <pre class="example">          cellfun (@atan2, {1, 0}, {0, 1})
 
78
          ans = [1.57080   0.00000]
 
79
</pre>
 
80
        <p>Note that the default output argument is an array of the same size as the
 
81
input arguments.
 
82
 
 
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
 
87
 
 
88
     <pre class="example">          cellfun ("tolower(x)", {"Foo", "Bar", "FooBar"},
 
89
                   "UniformOutput",false)
 
90
           ans = {"foo", "bar", "foobar"}
 
91
</pre>
 
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
 
94
 
 
95
     <pre class="example">          function [...] = errfunc (<var>s</var>, ...)
 
96
</pre>
 
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
 
102
 
 
103
     <pre class="example">          function y = foo (s, x), y = NaN; endfunction
 
104
          cellfun (@factorial, {-1,2},'ErrorHandler',@foo)
 
105
           ans = [NaN 2]
 
106
</pre>
 
107
        <pre class="sp">
 
108
     
 
109
     </pre>
 
110
     <strong>See also:</strong> isempty, islogical, isreal, length, ndims, numel, size, isclass. 
 
111
</p></blockquote></div>
 
112
 
 
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.
 
116
 
 
117
   <p><a name="doc_002dcell2mat"></a>
 
118
 
 
119
<div class="defun">
 
120
&mdash; 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.
 
125
        <pre class="sp">
 
126
     
 
127
     </pre>
 
128
     <strong>See also:</strong> mat2cell, num2cell. 
 
129
</p></blockquote></div>
 
130
 
 
131
   <p><a name="doc_002dcell2struct"></a>
 
132
 
 
133
<div class="defun">
 
134
&mdash; 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>.
 
138
 
 
139
     <pre class="example">          A = cell2struct ({'Peter', 'Hannah', 'Robert';
 
140
                             185, 170, 168},
 
141
                           {'Name','Height'}, 1);
 
142
          A(1)
 
143
           ans =
 
144
                {
 
145
                  Height = 185
 
146
                  Name   = Peter
 
147
                }
 
148
</pre>
 
149
        </blockquote></div>
 
150
 
 
151
   </body></html>
 
152