Mercurial > hg > octave-lyh
annotate scripts/general/structfun.m @ 10793:be55736a0783
Grammarcheck the documentation from m-files.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 18 Jul 2010 20:35:16 -0700 |
parents | a8ce6bdecce5 |
children | a4f482e66b65 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2007, 2008, 2009 David Bateman |
10132
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
2 ## Copyright (C) 2010 VZLU Prague |
6863 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
6863 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6863 | 19 |
20 ## -*- texinfo -*- | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefn {Function File} {} structfun (@var{func}, @var{s}) |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
22 ## @deftypefnx {Function File} {[@var{a}, @dots{}] =} structfun (@dots{}) |
8507 | 23 ## @deftypefnx {Function File} {} structfun (@dots{}, "ErrorHandler", @var{errfunc}) |
24 ## @deftypefnx {Function File} {} structfun (@dots{}, "UniformOutput", @var{val}) | |
6863 | 25 ## |
26 ## Evaluate the function named @var{name} on the fields of the structure | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## @var{s}. The fields of @var{s} are passed to the function @var{func} |
6863 | 28 ## individually. |
29 ## | |
30 ## @code{structfun} accepts an arbitrary function @var{func} in the form of | |
31 ## an inline function, function handle, or the name of a function (in a | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
32 ## character string). In the case of a character string argument, the |
6863 | 33 ## function must accept a single argument named @var{x}, and it must return |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
34 ## a string value. If the function returns more than one argument, they are |
6863 | 35 ## returned as separate output variables. |
36 ## | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
37 ## If the parameter "UniformOutput" is set to true (the default), then the |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
38 ## function |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
7208
diff
changeset
|
39 ## must return a single element which will be concatenated into the |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
40 ## return value. If "UniformOutput" is false, the outputs are placed into a |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
41 ## structure |
6863 | 42 ## with the same fieldnames as the input structure. |
43 ## | |
44 ## @example | |
45 ## @group | |
46 ## s.name1 = "John Smith"; | |
47 ## s.name2 = "Jill Jones"; | |
8507 | 48 ## structfun (@@(x) regexp (x, '(\w+)$', "matches")@{1@}, s, |
49 ## "UniformOutput", false) | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
50 ## @result{} |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
51 ## @{ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
52 ## name1 = Smith |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
53 ## name2 = Jones |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
54 ## @} |
6863 | 55 ## @end group |
56 ## @end example | |
57 ## | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
58 ## Given the parameter "ErrorHandler", @var{errfunc} defines a function to |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
59 ## call in case @var{func} generates an error. The form of the function is |
6863 | 60 ## |
61 ## @example | |
62 ## function [@dots{}] = errfunc (@var{se}, @dots{}) | |
63 ## @end example | |
64 ## | |
65 ## where there is an additional input argument to @var{errfunc} relative to | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
66 ## @var{func}, given by @var{se}. This is a structure with the elements |
8507 | 67 ## "identifier", "message" and "index", giving respectively the error |
6863 | 68 ## identifier, the error message, and the index into the input arguments |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
69 ## of the element that caused the error. For an example on how to use |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
70 ## an error handler, @pxref{doc-cellfun, @code{cellfun}}. |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
71 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
72 ## @seealso{cellfun, arrayfun, spfun} |
6863 | 73 ## @end deftypefn |
74 | |
75 function varargout = structfun (fun, s, varargin); | |
10134
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
76 |
6863 | 77 if (nargin < 2) |
78 print_usage (); | |
79 endif | |
80 | |
10134
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
81 nargs = length (varargin); |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
82 |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
83 recognized_opts = {"UniformOutput", "ErrorHandler"}; |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
84 uo_str = recognized_opts{1}; |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
85 |
10132
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
86 uniform_output = true; |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
87 |
10134
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
88 while (nargs >= 2) |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
89 opt_match = strcmpi (varargin{nargs-1}, recognized_opts); |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
90 if (opt_match(1)) |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
91 uniform_output = varargin{nargs}; |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
92 endif |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
93 if (any (opt_match)) |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
94 nargs -= 2; |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
95 else |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
96 break; |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
97 endif |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
98 endwhile |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
99 |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
100 if (nargs > 0) |
be13fa20656a
error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10132
diff
changeset
|
101 error ("structfun: invalid options"); |
10132
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
102 endif |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
103 |
6863 | 104 varargout = cell (max ([nargout, 1]), 1); |
7208 | 105 [varargout{:}] = cellfun (fun, struct2cell (s), varargin{:}); |
6863 | 106 |
10132
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
107 if (! uniform_output) |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
108 varargout = cellfun (@cell2struct, varargout, {fieldnames(s)}, {1}, uo_str, false); |
6863 | 109 endif |
110 endfunction | |
111 | |
112 | |
113 %!test | |
114 %! s.name1 = "John Smith"; | |
115 %! s.name2 = "Jill Jones"; | |
116 %! l.name1 = "Smith"; | |
117 %! l.name2 = "Jones"; | |
8507 | 118 %! o = structfun (@(x) regexp (x, '(\w+)$', "matches"){1}, s, |
10549 | 119 %! "UniformOutput", false); |
6863 | 120 %! assert (o, l); |
10132
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
121 |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
122 %!function [a, b] = twoouts (x) |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
123 %! a = x + x; |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
124 %! b = x * x; |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
125 |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
126 %!test |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
127 %! s = struct ("a", {1, 2, 3}, "b", {4, 5, 6}); |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
128 %! c(1:2, 1, 1) = [2; 8]; |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
129 %! c(1:2, 1, 2) = [4; 10]; |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
130 %! c(1:2, 1, 3) = [6; 12]; |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
131 %! d(1:2, 1, 1) = [1; 16]; |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
132 %! d(1:2, 1, 2) = [4; 25]; |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
133 %! d(1:2, 1, 3) = [9; 36]; |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
134 %! [aa, bb] = structfun(@twoouts, s); |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
135 %! assert(aa, c); |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
136 %! assert(bb, d); |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
137 |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
138 %!test |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
139 %! s = struct ("a", {1, 2, 3}, "b", {4, 5, 6}); |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
140 %! c = struct ("a", {2, 4, 6}, "b", {8, 10, 12}); |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
141 %! d = struct ("a", {1, 4, 9}, "b", {16, 25, 36}); |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
142 %! [aa, bb] = structfun(@twoouts, s, "uniformoutput", false); |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
143 %! assert(aa, c); |
aa0f575cf39b
improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
10122
diff
changeset
|
144 %! assert(bb, d); |