annotate scripts/general/structfun.m @ 10549:95c3e38098bf

Untabify .m scripts
author Rik <code@nomad.inbox5.com>
date Fri, 23 Apr 2010 11:28:50 -0700
parents be13fa20656a
children a8ce6bdecce5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8828
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
3 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
4 ## This file is part of Octave.
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
5 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
9 ## your option) any later version.
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
10 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
14 ## General Public License for more details.
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
15 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
18 ## <http://www.gnu.org/licenses/>.
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
19
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
20 ## -*- texinfo -*-
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
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
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8347
diff changeset
23 ## @deftypefnx {Function File} {} structfun (@dots{}, "ErrorHandler", @var{errfunc})
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8347
diff changeset
24 ## @deftypefnx {Function File} {} structfun (@dots{}, "UniformOutput", @var{val})
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
25 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
28 ## individually.
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
29 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
30 ## @code{structfun} accepts an arbitrary function @var{func} in the form of
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
35 ## returned as separate output variables.
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
36 ##
8828
8463d1a2e544 Doc fixes.
Brian Gough <bjg@network-theory.co.uk>
parents: 8507
diff changeset
37 ## If the parameter "UniformOutput" is set to true (the default), then the function
8347
fa78cb8d8a5c corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents: 7208
diff changeset
38 ## must return a single element which will be concatenated into the
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9036
diff changeset
39 ## return value. If "UniformOutput" is false, the outputs are placed into a structure
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
40 ## with the same fieldnames as the input structure.
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
41 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
42 ## @example
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
43 ## @group
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
44 ## s.name1 = "John Smith";
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
45 ## s.name2 = "Jill Jones";
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8347
diff changeset
46 ## structfun (@@(x) regexp (x, '(\w+)$', "matches")@{1@}, s,
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8347
diff changeset
47 ## "UniformOutput", false)
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9036
diff changeset
48 ## @result{}
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9036
diff changeset
49 ## @{
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9036
diff changeset
50 ## name1 = Smith
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9036
diff changeset
51 ## name2 = Jones
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9036
diff changeset
52 ## @}
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
53 ## @end group
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
54 ## @end example
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
55 ##
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9036
diff changeset
56 ## 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
57 ## call in case @var{func} generates an error. The form of the function is
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
58 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
59 ## @example
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
60 ## function [@dots{}] = errfunc (@var{se}, @dots{})
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
61 ## @end example
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
62 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
63 ## 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
64 ## @var{func}, given by @var{se}. This is a structure with the elements
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8347
diff changeset
65 ## "identifier", "message" and "index", giving respectively the error
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
66 ## 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
67 ## 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
68 ## 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
69 ##
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
70 ## @seealso{cellfun, arrayfun}
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
71 ## @end deftypefn
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
72
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
73 function varargout = structfun (fun, s, varargin);
10134
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
74
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
75 if (nargin < 2)
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
76 print_usage ();
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
77 endif
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
78
10134
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
79 nargs = length (varargin);
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
80
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
81 recognized_opts = {"UniformOutput", "ErrorHandler"};
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
82 uo_str = recognized_opts{1};
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
83
10132
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
84 uniform_output = true;
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
85
10134
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
86 while (nargs >= 2)
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
87 opt_match = strcmpi (varargin{nargs-1}, recognized_opts);
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
88 if (opt_match(1))
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
89 uniform_output = varargin{nargs};
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
90 endif
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
91 if (any (opt_match))
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
92 nargs -= 2;
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
93 else
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
94 break;
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
95 endif
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
96 endwhile
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
97
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
98 if (nargs > 0)
be13fa20656a error on invalid opts in structfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10132
diff changeset
99 error ("structfun: invalid options");
10132
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
100 endif
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
101
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
102 varargout = cell (max ([nargout, 1]), 1);
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7097
diff changeset
103 [varargout{:}] = cellfun (fun, struct2cell (s), varargin{:});
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
104
10132
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
105 if (! uniform_output)
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
106 varargout = cellfun (@cell2struct, varargout, {fieldnames(s)}, {1}, uo_str, false);
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
107 endif
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
108 endfunction
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
109
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
110
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
111 %!test
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
112 %! s.name1 = "John Smith";
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
113 %! s.name2 = "Jill Jones";
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
114 %! l.name1 = "Smith";
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
115 %! l.name2 = "Jones";
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8347
diff changeset
116 %! o = structfun (@(x) regexp (x, '(\w+)$', "matches"){1}, s,
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10134
diff changeset
117 %! "UniformOutput", false);
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
118 %! assert (o, l);
10132
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
119
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
120 %!function [a, b] = twoouts (x)
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
121 %! a = x + x;
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
122 %! b = x * x;
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
123
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
124 %!test
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
125 %! 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
126 %! c(1:2, 1, 1) = [2; 8];
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
127 %! c(1:2, 1, 2) = [4; 10];
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
128 %! c(1:2, 1, 3) = [6; 12];
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
129 %! d(1:2, 1, 1) = [1; 16];
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
130 %! d(1:2, 1, 2) = [4; 25];
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
131 %! d(1:2, 1, 3) = [9; 36];
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
132 %! [aa, bb] = structfun(@twoouts, s);
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
133 %! assert(aa, c);
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
134 %! assert(bb, d);
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
135
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
136 %!test
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
137 %! 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
138 %! 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
139 %! 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
140 %! [aa, bb] = structfun(@twoouts, s, "uniformoutput", false);
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
141 %! assert(aa, c);
aa0f575cf39b improve structfun's Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
142 %! assert(bb, d);