help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Multi-return functions as arguments


From: John W. Eaton
Subject: RE: Multi-return functions as arguments
Date: Fri, 18 Sep 1998 14:05:08 -0500 (CDT)

On 18-Sep-1998, (Ted Harding) <address@hidden> wrote:

| Meanwhile (though I guess from the above the answer is probably "no")
| is there any variant of "y=G(F3(u))" whereby G can access all three of
| the values returned by F3, instead of just the "top" one, i.e. x1?
| 
| (The only way I can think of is to use "F3" as argument and use "eval"
| inside G: y = G("F3",u) -- better than nothing, but with a tendency to be
| slow. Even then, it's not obvious how to get at the returned values
| inside G.)

Probably you would want to use feval() instead of eval(), but even so,
you would still have to know in advance how many arguments to expect
from each function.  So you could use something like this:

  function y = G (f, arg, expected_nargout)

    if (expected_nargout == 1)
      t1 = feval (f, arg);
      # compute y based on t1

    elseif (expected_nargout == 2)
      [t1, t2] = feval (f, arg);
      # compute y based on t1 and t2

    elseif (expected_nargout == 3)
      [t1, t2, t3] = feval (f, arg);
      # compute y based on t1, t2, and t3

    ...

    else
      error (...);

    endif

  endfunction


The reason that there is no way for you to ask for all the output
arguments from a function is that functions can decide to do different
things based on the requested number of outputs.  For example,

  S = svd (A)

returns the singular values of A in a vector, and

  [U, S, V] = svd (A)

returns matrices containing the left and right singular vectors (U and
V) and the singular values in a diagonal matrix (S).  It's not as
simple as just having svd() always produce three results and allowing
you to ignore some of them.  So asking a function like svd() for `all
outputs' is ambiguous.

FWIW, I think this feature is a fundamental flaw in the design of the
language.  It's unfortunate, but we are stuck with it if we want
Matlab compatibility.

jwe



reply via email to

[Prev in Thread] Current Thread [Next in Thread]