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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} structfun (@var{func}, @var{s}) |
|
22 ## @deftypefnx {Function File} {[@var{a}, @var{b}] =} structfun (@dots{}) |
|
23 ## @deftypefnx {Function File} {} structfun (@dots{}, 'ErrorHandler', @var{errfunc}) |
|
24 ## @deftypefnx {Function File} {} structfun (@dots{}, 'UniformOutput', @var{val}) |
|
25 ## |
|
26 ## Evaluate the function named @var{name} on the fields of the structure |
7001
|
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 |
|
32 ## character string). In the case of a character string argument, the |
|
33 ## function must accept a single argument named @var{x}, and it must return |
|
34 ## a string value. If the function returns more than one argument, they are |
|
35 ## returned as separate output variables. |
|
36 ## |
|
37 ## If the param 'UniformOutput' is set to true (the default), then the function |
|
38 ## must return either a single element which will be concatenated into the |
|
39 ## return value. If 'UniformOutput is false, the outputs placed in a structure |
|
40 ## with the same fieldnames as the input structure. |
|
41 ## |
|
42 ## @example |
|
43 ## @group |
|
44 ## s.name1 = "John Smith"; |
|
45 ## s.name2 = "Jill Jones"; |
|
46 ## structfun (@{x@} regexp (x, '(\w+)$', 'matches')@{1@}, s, |
|
47 ## 'UniformOutput', false) |
|
48 ## @end group |
|
49 ## @end example |
|
50 ## |
|
51 ## Given the parameter 'ErrorHandler', then @var{errfunc} defines a function to |
|
52 ## call in case @var{func} generates an error. The form of the function is |
|
53 ## |
|
54 ## @example |
|
55 ## function [@dots{}] = errfunc (@var{se}, @dots{}) |
|
56 ## @end example |
|
57 ## |
|
58 ## where there is an additional input argument to @var{errfunc} relative to |
|
59 ## @var{func}, given by @var{se}. This is a structure with the elements |
|
60 ## 'identifier', 'message' and 'index', giving respectively the error |
|
61 ## identifier, the error message, and the index into the input arguments |
|
62 ## of the element that caused the error. |
|
63 ## @seealso{cellfun, arrayfun} |
|
64 ## @end deftypefn |
|
65 |
|
66 function varargout = structfun (fun, s, varargin); |
|
67 if (nargin < 2) |
|
68 print_usage (); |
|
69 endif |
|
70 |
|
71 varargout = cell (max ([nargout, 1]), 1); |
|
72 [varargout{:}] = cellfun (fun, struct2cell (s), varargin {:}); |
|
73 |
|
74 if (iscell (varargout{1})) |
|
75 [varargout{:}] = cell2struct (varargout{1}, fieldnames(s), 1); |
|
76 endif |
|
77 endfunction |
|
78 |
|
79 |
|
80 %!test |
|
81 %! s.name1 = "John Smith"; |
|
82 %! s.name2 = "Jill Jones"; |
|
83 %! l.name1 = "Smith"; |
|
84 %! l.name2 = "Jones"; |
|
85 %! o = structfun (@(x) regexp (x, '(\w+)$', 'matches'){1}, s, |
|
86 %! 'UniformOutput', false); |
|
87 %! assert (o, l); |