help-octave
[Top][All Lists]
Advanced

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

Re: Synonym


From: Tony Richardson
Subject: Re: Synonym
Date: Wed, 8 Apr 2020 11:59:08 -0500



On Wed, Apr 8, 2020 at 11:07 AM John W. Eaton <address@hidden> wrote:
On 4/8/20 11:46 AM, "Markus Mützel" wrote:

> I don't think it is necessary to initialize varargout. It should be initialized to the correct length. [1]
> This should be enough:
>
> function varargout = short (varargin)
>    [varargout{:}] = verylongname (varargin{:});
> endfunction

Can you verify that this function actually works in Matlab (see below
for simple example)?

> [1]: https://de.mathworks.com/help/matlab/ref/varargout.html

The example on this page uses

   [varargout{1:nargout}] = ...

which does work in Octave.  But

   [varargout{:}] = ...

fails in Octave unless varargout is explicitly initialized.

And there is another example on that page that shows this:

function varargout = variableNumInputAndOutput(varargin)
     disp(['Number of provided inputs: ' num2str(length(varargin))])
     disp(['Number of requested outputs: ' num2str(nargout)])

If varargout is initialized when the function is called, then why use
length (varargin) instead of nargin and nargout instead of length
(varargout)?

Can you try the following in Matlab to verify that varargout is initialized?

function varargout = xsvd (varargin)
   numel (varargin)
   numel (varargout)
   [varargout{:}] = svd (varargin{:});
end

and then call it with

   xsvd (rand (2))
   s = xsvd (rand (2))
   [u, s, v] = xsvd (rand (2))

Does that work?  If so, I'll fix Octave to match that behavior.

jwe

I've noticed that the two cases:
   varargout = cell (nargout, 1);
   [varargout{:}] = svd(varargin{:}); 

and
   [varargout{1:nargout}] = svd(varargin{:}); 

behave differently when called with no output arguments: xsvd(rand(2)).
The first case returns nothing i.e. ans is not set, while the second case seems to behave normally.

Tony Richardson


reply via email to

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