annotate scripts/general/structfun.m @ 6863:3c64128e621c

[project @ 2007-09-05 07:52:48 by dbateman]
author dbateman
date Wed, 05 Sep 2007 07:53:45 +0000
parents
children 8b0cfeb06365
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
1 ## Copyright (C) 2007 David Bateman
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
2 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
3 ## This file is part of Octave.
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
4 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
5 ## 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
6 ## under the terms of the GNU General Public License as published by
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
7 ## the Free Software Foundation; either version 2, or (at your option)
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
8 ## any later version.
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
9 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
10 ## 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
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
13 ## General Public License for more details.
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
14 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, write to the Free
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
18 ## 02110-1301, USA.
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})
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
22 ## @deftypefnx {Function File} {[@var{a}, @var{b}] =} structfun (@dots{})
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
23 ## @deftypefnx {Function File} {} structfun (@dots{}, 'ErrorHandler', @var{errfunc})
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
24 ## @deftypefnx {Function File} {} structfun (@dots{}, 'UniformOutput', @var{val})
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
27 ## @var{s}. The fields of @var{s} are passed the the function @var{func}
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
32 ## character string). In the case of a character string argument, the
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
34 ## a string value. If the function returns more than one argument, they are
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 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
37 ## If the param 'UniformOutput' is set to true (the default), then the function
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
38 ## must return either a single element which will be concatenated into the
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
39 ## return value. If 'UniformOutput is false, the outputs placed in a structure
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";
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
46 ## structfun (@{x@} regexp (x, '(\w+)$', 'matches')@{1@}, s,
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
47 ## 'UniformOutput', false)
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
48 ## @end group
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
49 ## @end example
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
50 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
51 ## Given the parameter 'ErrorHandler', then @var{errfunc} defines a function to
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
52 ## call in case @var{func} generates an error. The form of the function is
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
53 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
54 ## @example
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
55 ## function [@dots{}] = errfunc (@var{se}, @dots{})
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
56 ## @end example
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
57 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
58 ## where there is an additional input argument to @var{errfunc} relative to
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
59 ## @var{func}, given by @var{se}. This is a structure with the elements
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
60 ## 'identifier', 'message' and 'index', giving respectively the error
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
61 ## identifier, the error message, and the index into the input arguments
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
62 ## of the element that caused the error.
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
63 ## @seealso{cellfun, arrayfun}
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
64 ## @end deftypefn
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
65
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
66 function varargout = structfun (fun, s, varargin);
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
67 if (nargin < 2)
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
68 print_usage ();
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
69 endif
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
70
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
71 varargout = cell (max ([nargout, 1]), 1);
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
72 [varargout{:}] = cellfun (fun, struct2cell (s), varargin {:});
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
73
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
74 if (iscell (varargout{1}))
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
75 [varargout{:}] = cell2struct (varargout{1}, fieldnames(s), 1);
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
76 endif
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
77 endfunction
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
78
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
79
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
80 %!test
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
81 %! s.name1 = "John Smith";
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
82 %! s.name2 = "Jill Jones";
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
83 %! l.name1 = "Smith";
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
84 %! l.name2 = "Jones";
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
85 %! o = structfun (@(x) regexp (x, '(\w+)$', 'matches'){1}, s,
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
86 %! 'UniformOutput', false);
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
87 %! assert (o, l);