[Top][All Lists]

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

Re: varargout{:} = func() ?

From: Jaroslav Hajek
Subject: Re: varargout{:} = func() ?
Date: Wed, 24 Feb 2010 17:33:06 +0100

On Wed, Feb 24, 2010 at 3:59 PM, forkandwait <address@hidden> wrote:
>>   v = cell (expected_outputs, 1);
>>   [v{:}] = func (...);
>> should work in any recent version of Octave.
> It does.
>>   clear v;
>>   [v{:}] = func (...);
> Hmm.  But they said it would on the newsgroup ;).  To be fair, they may have
> assumed that one would know pre-sizing the cell array.
>> will not work, because Octave can't determine nargout for the function
>> call.  I expect this is also true in Matlab.
> I guess there is no notation in case one doesn't know the number of expected
> outputs?  I am just curious at this point.

As has been already stated, the number of lvalue references in a
multiple assignment has to be computed *prior* to evaluating the rhs.
The rhs must know the nargout, because it is used in functions, both
user and built-ins.

A user function may assign more output params than actually needed; in
that case, the rest is ignored. The opposite is impractical in most
situations. Consider a function like
function [f, J] = my_func (x)
  f = # function value
  J = # jacobian

here the user doesn't bother to check for nargout. Would you be
surprised if the expression
[other_matrix, my_func(y)] actually concatenated *both* f and J? I
would. That is what would happen if the excess output arguments
propagated out of the function.

By the way, this is why overloaded numel () plays an important role in
overloading subsref and subsasgn for user classes, if they should be
able to handle cs-lists. In indexed assignment, numel () is used to
compute the number of lvalues.


RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic

reply via email to

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