6863
|
1 ## Copyright (C) 2007 David Bateman |
|
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{}) |
|
22 ## @deftypefnx {Function File} {} structfun (@dots{}, 'ErrorHandler', @var{errfunc}) |
|
23 ## @deftypefnx {Function File} {} structfun (@dots{}, 'UniformOutput', @var{val}) |
|
24 ## |
|
25 ## Evaluate the function named @var{name} on the fields of the structure |
7001
|
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 |
|
31 ## character string). In the case of a character string argument, the |
|
32 ## function must accept a single argument named @var{x}, and it must return |
|
33 ## a string value. If the function returns more than one argument, they are |
|
34 ## returned as separate output variables. |
|
35 ## |
|
36 ## If the param 'UniformOutput' is set to true (the default), then the function |
|
37 ## must return either a single element which will be concatenated into the |
|
38 ## return value. If 'UniformOutput is false, the outputs placed in a structure |
|
39 ## with the same fieldnames as the input structure. |
|
40 ## |
|
41 ## @example |
|
42 ## @group |
|
43 ## s.name1 = "John Smith"; |
|
44 ## s.name2 = "Jill Jones"; |
|
45 ## structfun (@{x@} regexp (x, '(\w+)$', 'matches')@{1@}, s, |
|
46 ## 'UniformOutput', false) |
|
47 ## @end group |
|
48 ## @end example |
|
49 ## |
|
50 ## Given the parameter 'ErrorHandler', then @var{errfunc} defines a function to |
|
51 ## call in case @var{func} generates an error. The form of the function is |
|
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 |
|
58 ## @var{func}, given by @var{se}. This is a structure with the elements |
|
59 ## 'identifier', 'message' and 'index', giving respectively the error |
|
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); |
|
71 [varargout{:}] = cellfun (fun, struct2cell (s), varargin {:}); |
|
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"; |
|
84 %! o = structfun (@(x) regexp (x, '(\w+)$', 'matches'){1}, s, |
|
85 %! 'UniformOutput', false); |
|
86 %! assert (o, l); |