view 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
line wrap: on
line source

## Copyright (C) 2007 David Bateman
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {} structfun (@var{func}, @var{s})
## @deftypefnx {Function File} {[@var{a}, @var{b}] =} structfun (@dots{})
## @deftypefnx {Function File} {} structfun (@dots{}, "ErrorHandler", @var{errfunc})
## @deftypefnx {Function File} {} structfun (@dots{}, "UniformOutput", @var{val})
## 
## Evaluate the function named @var{name} on the fields of the structure
## @var{s}. The fields of @var{s} are passed to the function @var{func}
## individually.
##
## @code{structfun} accepts an arbitrary function @var{func} in the form of 
## an inline function, function handle, or the name of a function (in a 
## character string). In the case of a character string argument, the 
## function must accept a single argument named @var{x}, and it must return 
## a string value. If the function returns more than one argument, they are
## returned as separate output variables.
##
## If the parameter "UniformOutput" is set to true (the default), then the function
## must return a single element which will be concatenated into the
## return value. If "UniformOutput" is false, the outputs placed in a structure
## with the same fieldnames as the input structure.
## 
## @example
## @group
## s.name1 = "John Smith"; 
## s.name2 = "Jill Jones"; 
## structfun (@@(x) regexp (x, '(\w+)$', "matches")@{1@}, s, 
##            "UniformOutput", false)
## @end group
## @end example
## 
## Given the parameter "ErrorHandler", then @var{errfunc} defines a function to
## call in case @var{func} generates an error. The form of the function is
## 
## @example
## function [@dots{}] = errfunc (@var{se}, @dots{})
## @end example
## 
## where there is an additional input argument to @var{errfunc} relative to
## @var{func}, given by @var{se}. This is a structure with the elements
## "identifier", "message" and "index", giving respectively the error
## identifier, the error message, and the index into the input arguments
## of the element that caused the error.
## @seealso{cellfun, arrayfun}
## @end deftypefn

function varargout = structfun (fun, s, varargin);
  if (nargin < 2)
    print_usage ();
  endif

  varargout = cell (max ([nargout, 1]), 1);
  [varargout{:}] = cellfun (fun, struct2cell (s), varargin{:});

  if (iscell (varargout{1}))
    [varargout{:}] = cell2struct (varargout{1}, fieldnames(s), 1);
  endif
endfunction


%!test
%! s.name1 = "John Smith"; 
%! s.name2 = "Jill Jones"; 
%! l.name1 = "Smith";
%! l.name2 = "Jones";
%! o = structfun (@(x) regexp (x, '(\w+)$', "matches"){1}, s, 
%!		  "UniformOutput", false);
%! assert (o, l);