Mercurial > hg > octave-lyh
annotate scripts/general/structfun.m @ 10122:9d1a14e12431
Update docs and add tests for container functions
author | Thorsten Meyer <thorsten.meyier@gmx.de> |
---|---|
date | Sun, 17 Jan 2010 13:31:42 +0100 |
parents | 58604c45ca74 |
children | aa0f575cf39b |
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}) | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{a}, @dots{}] =} 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 |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
38 ## return value. If "UniformOutput" is false, the outputs are placed into 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) | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
47 ## @result{} |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
48 ## @{ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
49 ## name1 = Smith |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
50 ## name2 = Jones |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
51 ## @} |
6863 | 52 ## @end group |
53 ## @end example | |
54 ## | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9036
diff
changeset
|
55 ## 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
|
56 ## call in case @var{func} generates an error. The form of the function is |
6863 | 57 ## |
58 ## @example | |
59 ## function [@dots{}] = errfunc (@var{se}, @dots{}) | |
60 ## @end example | |
61 ## | |
62 ## 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
|
63 ## @var{func}, given by @var{se}. This is a structure with the elements |
8507 | 64 ## "identifier", "message" and "index", giving respectively the error |
6863 | 65 ## 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
|
66 ## 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
|
67 ## 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
|
68 ## |
6863 | 69 ## @seealso{cellfun, arrayfun} |
70 ## @end deftypefn | |
71 | |
72 function varargout = structfun (fun, s, varargin); | |
73 if (nargin < 2) | |
74 print_usage (); | |
75 endif | |
76 | |
77 varargout = cell (max ([nargout, 1]), 1); | |
7208 | 78 [varargout{:}] = cellfun (fun, struct2cell (s), varargin{:}); |
6863 | 79 |
80 if (iscell (varargout{1})) | |
81 [varargout{:}] = cell2struct (varargout{1}, fieldnames(s), 1); | |
82 endif | |
83 endfunction | |
84 | |
85 | |
86 %!test | |
87 %! s.name1 = "John Smith"; | |
88 %! s.name2 = "Jill Jones"; | |
89 %! l.name1 = "Smith"; | |
90 %! l.name2 = "Jones"; | |
8507 | 91 %! o = structfun (@(x) regexp (x, '(\w+)$', "matches"){1}, s, |
92 %! "UniformOutput", false); | |
6863 | 93 %! assert (o, l); |