Mercurial > hg > octave-lyh
annotate scripts/general/structfun.m @ 8828:8463d1a2e544
Doc fixes.
* 2]$$. => 2].$$
* @var{extrapval} => @var{extrapval}.
* call helloworld.oct => called @file{helloworld.oct}
* @itemize => @table @code
* shows. => shows:
* save => @code{save}
* @ref{Breakpoints} => @pxref{Breakpoints}
* add @noindent following example
* which is computed => and compute it
* clarify wording
* remove comma
* good => well
* set => number
* by writing => with the command
* has the option of directly calling => can call
* [-like-] {+of the right size,+}
* solvers => routines
* handle => test for
* add introductory section
* add following
* {+the+} [0..bitmax] => [0,bitmax]
* of the => with
* number => value
* add usual
* Besides when doing comparisons, logical => Logical {+also+}
* array comparison => array, comparisons
* param => parameter
* works very similar => is similar
* strings, => strings
* most simple => simplest
* easier => more easily
* like => as
* called => called,
* clarify wording
* you should simply type => use
* clarify wording
* means => way
* equally => also
* [-way much-] {+way+}
* add with mean value parameter given by the first argument, @var{l}
* add Functions described as @dfn{mapping functions} apply the given
operation to each element when given a matrix argument.
* in this brief introduction => here
* It is worth noticing => Note
* add following
* means => ways
author | Brian Gough <bjg@network-theory.co.uk> |
---|---|
date | Fri, 20 Feb 2009 11:17:01 -0500 |
parents | cadc73247d65 |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2007 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 | |
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 ## | |
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 |
8507 | 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 |
6863 | 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 | |
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); |