1 ## Copyright (C) 2012 Jordi GutiƩrrez Hermoso
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} nthargout (@var{n}, @var{func}, @dots{})
21 ## @deftypefnx {Function File} {} nthargout (@var{n}, @var{ntot}, @var{func}, @dots{})
22 ## Return the @var{n}th output argument of function given by the
23 ## function handle or string @var{func}. Any arguments after @var{func}
24 ## are passed to @var{func}. The total number of arguments to call
25 ## @var{func} with can be passed in @var{ntot}; by default @var{ntot}
26 ## is @var{n}. The input @var{n} can also be a vector of indices of the
27 ## output, in which case the output will be a cell array of the
28 ## requested output arguments.
30 ## The intended use @code{nthargout} is to avoid intermediate variables.
31 ## For example, when finding the indices of the maximum entry of a
32 ## matrix, the following two compositions of nthargout
36 ## @var{m} = magic (5);
37 ## cell2mat (nthargout ([1, 2], @@ind2sub, size(@var{m}),
38 ## nthargout (2, @@max, @var{m}(:))))
44 ## are completely equivalent to the following lines:
48 ## @var{m} = magic(5);
49 ## [~, idx] = max (@var{M}(:));
50 ## [i, j] = ind2sub (size (@var{m}), idx);
56 ## It can also be helpful to have all output arguments in a single cell
57 ## in the following manner:
60 ## @var{USV} = nthargout ([1:3], @@svd, hilb (5));
63 ## @seealso{nargin, nargout, varargin, varargout, isargout}
66 ## Author: Jordi GutiƩrrez Hermoso
68 function out = nthargout (n, varargin)
73 if (isa (varargin{1}, "function_handle") || ischar (varargin{1}))
76 args = varargin(2:end);
77 elseif (isnumeric (varargin{1})
78 && (isa (varargin{2}, "function_handle") || ischar (varargin{2})))
81 args = varargin(3:end);
86 if (any (n != fix (n)) || ntot != fix (ntot) || any (n <= 0) || ntot <= 0)
87 error ("nthargout: N and NTOT must consist of positive integers")
90 outargs = cell (1, ntot);
93 [outargs{:}] = feval (func, args{:});
101 if (strfind ("some elements undefined in return list", err))
102 error ("nthargout: Too many output arguments: %d", ntot);
112 %!assert (nthargout ([1, 2], @ind2sub, size(m), nthargout (2, @max, m(:))), {5,3})
113 %!assert (nthargout (3, @find, m(m>20)), [23, 24, 25, 21, 22]')