Mercurial > hg > octave-lyh
annotate scripts/general/structfun.m @ 9141:c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Split section into "Exponents and Logarithms" and "Utility Functions"
Use Tex in many more of the doc strings for pretty printing in pdf format.
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Mon, 20 Apr 2009 17:16:09 -0700 |
parents | 58604c45ca74 |
children | 9d1a14e12431 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2007, 2008, 2009 David Bateman |
6863 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6863 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6863 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} structfun (@var{func}, @var{s}) | |
21 ## @deftypefnx {Function File} {[@var{a}, @var{b}] =} structfun (@dots{}) | |
8507 | 22 ## @deftypefnx {Function File} {} structfun (@dots{}, "ErrorHandler", @var{errfunc}) |
23 ## @deftypefnx {Function File} {} structfun (@dots{}, "UniformOutput", @var{val}) | |
6863 | 24 ## |
25 ## 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
|
26 ## @var{s}. The fields of @var{s} are passed to the function @var{func} |
6863 | 27 ## individually. |
28 ## | |
29 ## @code{structfun} accepts an arbitrary function @var{func} in the form of | |
30 ## 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
|
31 ## character string). In the case of a character string argument, the |
6863 | 32 ## 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
|
33 ## a string value. If the function returns more than one argument, they are |
6863 | 34 ## returned as separate output variables. |
35 ## | |
8828 | 36 ## 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
|
37 ## must return a single element which will be concatenated into the |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
38 ## return value. If "UniformOutput" is false, the outputs placed in a structure |
6863 | 39 ## with the same fieldnames as the input structure. |
40 ## | |
41 ## @example | |
42 ## @group | |
43 ## s.name1 = "John Smith"; | |
44 ## s.name2 = "Jill Jones"; | |
8507 | 45 ## structfun (@@(x) regexp (x, '(\w+)$', "matches")@{1@}, s, |
46 ## "UniformOutput", false) | |
6863 | 47 ## @end group |
48 ## @end example | |
49 ## | |
8507 | 50 ## Given the parameter "ErrorHandler", then @var{errfunc} defines a function to |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
51 ## call in case @var{func} generates an error. The form of the function is |
6863 | 52 ## |
53 ## @example | |
54 ## function [@dots{}] = errfunc (@var{se}, @dots{}) | |
55 ## @end example | |
56 ## | |
57 ## 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
|
58 ## @var{func}, given by @var{se}. This is a structure with the elements |
8507 | 59 ## "identifier", "message" and "index", giving respectively the error |
6863 | 60 ## identifier, the error message, and the index into the input arguments |
61 ## of the element that caused the error. | |
62 ## @seealso{cellfun, arrayfun} | |
63 ## @end deftypefn | |
64 | |
65 function varargout = structfun (fun, s, varargin); | |
66 if (nargin < 2) | |
67 print_usage (); | |
68 endif | |
69 | |
70 varargout = cell (max ([nargout, 1]), 1); | |
7208 | 71 [varargout{:}] = cellfun (fun, struct2cell (s), varargin{:}); |
6863 | 72 |
73 if (iscell (varargout{1})) | |
74 [varargout{:}] = cell2struct (varargout{1}, fieldnames(s), 1); | |
75 endif | |
76 endfunction | |
77 | |
78 | |
79 %!test | |
80 %! s.name1 = "John Smith"; | |
81 %! s.name2 = "Jill Jones"; | |
82 %! l.name1 = "Smith"; | |
83 %! l.name2 = "Jones"; | |
8507 | 84 %! o = structfun (@(x) regexp (x, '(\w+)$', "matches"){1}, s, |
85 %! "UniformOutput", false); | |
6863 | 86 %! assert (o, l); |