Synonym

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Synonym

Ian McCallion
What is the right syntax for providing a function which does nothing except call another function with the same parameters and returns the same values back.

The function below passes the parameters through, but getting returned values back does not work. I've tried various incantations without success

   function [varargout] = short(varargin)
       varargout = verylongame(varargin){:})
    endfunction

Cheers... Ian


Reply | Threaded
Open this post in threaded view
|

Re: Synonym

Doug Stewart-4


On Wed, Apr 8, 2020 at 7:35 AM Ian McCallion <[hidden email]> wrote:
What is the right syntax for providing a function which does nothing except call another function with the same parameters and returns the same values back.

The function below passes the parameters through, but getting returned values back does not work. I've tried various incantations without success

   function [varargout] = short(varargin)
       varargout = verylongame(varargin){:})
    endfunction

Cheers... Ian

this seems to work!

 function [varargout2] = short(varargin)
       varargout = verylongname(varargin{:})
       varargout2=varargout(:)
    endfunction

--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

RE: Synonym

Tony Richardson

 

From: Doug Stewart
To: Ian McCallion <[hidden email]>
Cc: Help GNU Octave <[hidden email]>
Subject: Re: Synonym

 

>> On Wed, Apr 8, 2020 at 7:35 AM Ian McCallion <[hidden email]> wrote:

>> What is the right syntax for providing a function which does nothing except call another function with the same parameters and returns the same values back.

>> 

>> The function below passes the parameters through, but getting returned values back does not work. I've tried various incantations without success

>> 

>>   function [varargout] = short(varargin)

>>       varargout = verylongame(varargin){:})

>>    endfunction

>> 

>> Cheers... Ian

>> 

> this seems to work!

>
> function [varargout2] = short(varargin)
>       varargout = verylongname(varargin{:})
>       varargout2=varargout(:)
>    endfunction
>

> 

> --

> DAS

 

Doug,

I could not get that to work when multiple output arguments are requested.  My attempt is below.  It’s kind of ugly, maybe someone can come up with a nicer method.

 

Tony

 

function varargout = short(varargin)

  usv = nthargout ([1:nargout()], @short, varargin{:});

   if isa(usv, "cell")

      for i = 1:nargout

        varargout{i} = usv{i};

      endfor

    else

      varargout{1} = usv;

    endif 

endfunction

 

 

 

 

 

 



Reply | Threaded
Open this post in threaded view
|

Re: Synonym

Doug Stewart-4


On Wed, Apr 8, 2020 at 8:28 AM Richardson, Anthony <[hidden email]> wrote:

 

From: Doug Stewart
To: Ian McCallion <[hidden email]>
Cc: Help GNU Octave <[hidden email]>
Subject: Re: Synonym

 

>> On Wed, Apr 8, 2020 at 7:35 AM Ian McCallion <[hidden email]> wrote:

>> What is the right syntax for providing a function which does nothing except call another function with the same parameters and returns the same values back.

>> 

>> The function below passes the parameters through, but getting returned values back does not work. I've tried various incantations without success

>> 

>>   function [varargout] = short(varargin)

>>       varargout = verylongame(varargin){:})

>>    endfunction

>> 

>> Cheers... Ian

>> 

> this seems to work!

>
> function [varargout2] = short(varargin)
>       varargout = verylongname(varargin{:})
>       varargout2=varargout(:)
>    endfunction
>

> 

> --

> DAS

 

Doug,

I could not get that to work when multiple output arguments are requested.  My attempt is below.  It’s kind of ugly, maybe someone can come up with a nicer method.

 

Tony

 

function varargout = short(varargin)

  usv = nthargout ([1:nargout()], @short, varargin{:});

   if isa(usv, "cell")

      for i = 1:nargout

        varargout{i} = usv{i};

      endfor

    else

      varargout{1} = usv;

    endif 

endfunction

 

 

 

 

 

 

Here is my testing code that worked:

a=1;
 function q=verylongname(x)
   q(1)={'hi'}
   q(2)={'to you'}
 endfunction
 
 
 
 function [varargout2] = short(varargin)
       varargout = verylongname(varargin{:})
       varargout2=varargout(:)
    endfunction
   
    varargout1=short()
  

put that in an M file and run it.
  

--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: Synonym

Ian McCallion
In reply to this post by Tony Richardson
On Wed, 8 Apr 2020 at 13:28, Richardson, Anthony <[hidden email]> wrote:

 

From: Doug Stewart
To: Ian McCallion <[hidden email]>
Cc: Help GNU Octave <[hidden email]>
Subject: Re: Synonym

 

>> On Wed, Apr 8, 2020 at 7:35 AM Ian McCallion <[hidden email]> wrote:

>> What is the right syntax for providing a function which does nothing except call another function with the same parameters and returns the same values back.

>> 

>> The function below passes the parameters through, but getting returned values back does not work. I've tried various incantations without success

>> 

>>   function [varargout] = short(varargin)

>>       varargout = verylongame(varargin){:})

>>    endfunction

>> 

>> Cheers... Ian

>> 

> this seems to work!

>
> function [varargout2] = short(varargin)
>       varargout = verylongname(varargin{:})
>       varargout2=varargout(:)
>    endfunction
 

Doug,

I could not get that to work when multiple output arguments are requested.  My attempt is below.  It’s kind of ugly, maybe someone can come up with a nicer method.

 

Tony

 

function varargout = short(varargin)

  usv = nthargout ([1:nargout()], @short, varargin{:});

   if isa(usv, "cell")

      for i = 1:nargout

        varargout{i} = usv{i};

      endfor

    else

      varargout{1} = usv;

    endif 

endfunction


Tony, I had the same problem with Doug's solution, but I don't understand yours as it does not call verylongname()

Help?

Cheers... Ian



Reply | Threaded
Open this post in threaded view
|

RE: Synonym

Tony Richardson

Sorry, the function name in the nthargout line should be verylongname not short.

Tony

 

From: Ian McCallion <[hidden email]>
Sent: Wednesday, April 8, 2020 7:52 AM
To: Richardson, Anthony <[hidden email]>
Cc: Doug Stewart <[hidden email]>; Help GNU Octave <[hidden email]>
Subject: Re: Synonym

 

On Wed, 8 Apr 2020 at 13:28, Richardson, Anthony <[hidden email]> wrote:

 

From: Doug Stewart
To: Ian McCallion <[hidden email]>
Cc: Help GNU Octave <[hidden email]>
Subject: Re: Synonym

 

>> On Wed, Apr 8, 2020 at 7:35 AM Ian McCallion <[hidden email]> wrote:

>> What is the right syntax for providing a function which does nothing except call another function with the same parameters and returns the same values back.

>> 

>> The function below passes the parameters through, but getting returned values back does not work. I've tried various incantations without success

>> 

>>   function [varargout] = short(varargin)

>>       varargout = verylongame(varargin){:})

>>    endfunction

>> 

>> Cheers... Ian

>> 

> this seems to work!

>
> function [varargout2] = short(varargin)
>       varargout = verylongname(varargin{:})
>       varargout2=varargout(:)
>    endfunction
 

Doug,

I could not get that to work when multiple output arguments are requested.  My attempt is below.  It’s kind of ugly, maybe someone can come up with a nicer method.

 

Tony

 

function varargout = short(varargin)

  usv = nthargout ([1:nargout()], @short, varargin{:});

   if isa(usv, "cell")

      for i = 1:nargout

        varargout{i} = usv{i};

      endfor

    else

      varargout{1} = usv;

    endif 

endfunction

 

Tony, I had the same problem with Doug's solution, but I don't understand yours as it does not call verylongname()

 

Help?

 

Cheers... Ian



Reply | Threaded
Open this post in threaded view
|

RE: Synonym

Tony Richardson
In reply to this post by Doug Stewart-4

Doug,

Your code works fine if verylongname returns a single cell argument, but I can’t get it to work when the function returns multiple output arguments (like the svd function for example).

Tony

 

From: Doug Stewart <[hidden email]>
Sent: Wednesday, April 8, 2020 7:38 AM
To: Richardson, Anthony <[hidden email]>
Cc: Ian McCallion <[hidden email]>; Help GNU Octave <[hidden email]>
Subject: Re: Synonym

 

 

 

On Wed, Apr 8, 2020 at 8:28 AM Richardson, Anthony <[hidden email]> wrote:

 

From: Doug Stewart
To: Ian McCallion <[hidden email]>
Cc: Help GNU Octave <[hidden email]>
Subject: Re: Synonym

 

>> On Wed, Apr 8, 2020 at 7:35 AM Ian McCallion <[hidden email]> wrote:

>> What is the right syntax for providing a function which does nothing except call another function with the same parameters and returns the same values back.

>> 

>> The function below passes the parameters through, but getting returned values back does not work. I've tried various incantations without success

>> 

>>   function [varargout] = short(varargin)

>>       varargout = verylongame(varargin){:})

>>    endfunction

>> 

>> Cheers... Ian

>> 

> this seems to work!

>
> function [varargout2] = short(varargin)
>       varargout = verylongname(varargin{:})
>       varargout2=varargout(:)
>    endfunction
>

> --

> DAS

 

Doug,

I could not get that to work when multiple output arguments are requested.  My attempt is below.  It’s kind of ugly, maybe someone can come up with a nicer method.

 

Tony

 

function varargout = short(varargin)

  usv = nthargout ([1:nargout()], @short, varargin{:});

   if isa(usv, "cell")

      for i = 1:nargout

        varargout{i} = usv{i};

      endfor

    else

      varargout{1} = usv;

    endif 

endfunction

 

 

 

 

 

 

Here is my testing code that worked:

 

a=1;
 function q=verylongname(x)
   q(1)={'hi'}
   q(2)={'to you'}
 endfunction
 
 
 
 function [varargout2] = short(varargin)
       varargout = verylongname(varargin{:})
       varargout2=varargout(:)
    endfunction
   
    varargout1=short()
  

 

put that in an M file and run it.

  

 

--

DASCertificate for 206392

 



Reply | Threaded
Open this post in threaded view
|

Re: Synonym

John W. Eaton
Administrator
In reply to this post by Ian McCallion
On 4/8/20 7:35 AM, Ian McCallion wrote:

> What is the right syntax for providing a function which does nothing
> except call another function with the same parameters and returns the
> same values back.
>
> The function below passes the parameters through, but getting returned
> values back does not work. I've tried various incantations without success
>
>     function [varargout] = short(varargin)
>         varargout = verylongame(varargin){:})
>      endfunction

Try

   function varargout = short (varargin)
     varargout = cell (nargout, 1);
     [varargout{:}] = verylongname (varargin{:});
   endfunction

Then verylongname will see the same value for nargout that short does
when it is called.  There are some forwarding functions like this in
Octave.  For example, bar.m and barh.m both forward to an internal
function private/__bar__.m.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: Synonym

Ian McCallion
On Wed, 8 Apr 2020 at 14:20, John W. Eaton <[hidden email]> wrote:
On 4/8/20 7:35 AM, Ian McCallion wrote:
> What is the right syntax for providing a function which does nothing
> except call another function with the same parameters and returns the
> same values back.
>
> The function below passes the parameters through, but getting returned
> values back does not work. I've tried various incantations without success
>
>     function [varargout] = short(varargin)
>         varargout = verylongame(varargin){:})
>      endfunction

Try

   function varargout = short (varargin)
     varargout = cell (nargout, 1);
     [varargout{:}] = verylongname (varargin{:});
   endfunction

Then verylongname will see the same value for nargout that short does
when it is called.  There are some forwarding functions like this in
Octave.  For example, bar.m and barh.m both forward to an internal
function private/__bar__.m.

Magic, thank you!!  I KNEW there should have been an appropriate syntax, but it had not occurred to me to initialise varargout to an appropriate sized cell array first.

Cheers... Ian




Reply | Threaded
Open this post in threaded view
|

Re: Synonym

Doug Stewart-4


On Wed, Apr 8, 2020 at 9:27 AM Ian McCallion <[hidden email]> wrote:
On Wed, 8 Apr 2020 at 14:20, John W. Eaton <[hidden email]> wrote:
On 4/8/20 7:35 AM, Ian McCallion wrote:
> What is the right syntax for providing a function which does nothing
> except call another function with the same parameters and returns the
> same values back.
>
> The function below passes the parameters through, but getting returned
> values back does not work. I've tried various incantations without success
>
>     function [varargout] = short(varargin)
>         varargout = verylongame(varargin){:})
>      endfunction

Try

   function varargout = short (varargin)
     varargout = cell (nargout, 1);
     [varargout{:}] = verylongname (varargin{:});
   endfunction

Then verylongname will see the same value for nargout that short does
when it is called.  There are some forwarding functions like this in
Octave.  For example, bar.m and barh.m both forward to an internal
function private/__bar__.m.

Magic, thank you!!  I KNEW there should have been an appropriate syntax, but it had not occurred to me to initialise varargout to an appropriate sized cell array first.

Cheers... Ian



with jwe's help  this now works

a=1;
 function [aa varargout]=verylongname(x)
   q(1)={'hi'}
   q(2)={'to you'}
   f(1)={'1, 2, 3'}
   f(2)={'like'}
   varargout{1}=q;
   varargout{2}=f;
   varargout
   aa=99
 endfunction
 
 
 
 function [varargout2] = short(varargin)
      varargout2 = cell (nargout, 2);
     [bb varargout2{:}] = verylongname (varargin{:});
       bb
    endfunction
   
    varargout1=short()
    

--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: Synonym

mmuetzel
In reply to this post by Ian McCallion
Am 08. April 2020 um 15:26 Uhr schrieb "Ian McCallion":

> On Wed, 8 Apr 2020 at 14:20, John W. Eaton <[hidden email][mailto:[hidden email]]> wrote:
> > On 4/8/20 7:35 AM, Ian McCallion wrote:
> > > What is the right syntax for providing a function which does nothing
> > > except call another function with the same parameters and returns the
> > > same values back.
> > >
> > > The function below passes the parameters through, but getting returned
> > > values back does not work. I've tried various incantations without success
> > >
> > >     function [varargout] = short(varargin)
> > >         varargout = verylongame(varargin){:})
> > >      endfunction
> >
> > Try
> >
> >    function varargout = short (varargin)
> >     varargout = cell (nargout, 1);
> >      [varargout{:}] = verylongname (varargin{:});
> >    endfunction
> >
> > Then verylongname will see the same value for nargout that short does
> > when it is called.  There are some forwarding functions like this in
> > Octave.  For example, bar.m and barh.m both forward to an internal
> > function private/__bar__.m.

> Magic, thank you!!  I KNEW there should have been an appropriate syntax, but it had not occurred to me to initialise varargout to an appropriate sized cell array first.

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


If it isn't, please report an incompatibility bug.

Markus

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


Reply | Threaded
Open this post in threaded view
|

Re: Synonym

John W. Eaton
Administrator
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



Reply | Threaded
Open this post in threaded view
|

Re: Synonym

Tony Richardson-4


On Wed, Apr 8, 2020 at 11:07 AM John W. Eaton <[hidden email]> 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 | Threaded
Open this post in threaded view
|

Re: Synonym

mmuetzel
In reply to this post by John W. Eaton
Am 08. April 2020 um 18:07 Uhr schrieb "John W. Eaton":

> 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.

It does not (tested with Matlab R2019b).
I thought it would be the expected behavior by reading this:
"When the function executes, varargout is a 1-by-N cell array, where N is the number of outputs requested after the explicitly declared outputs."

But that clearly seems to be *not* the case. I must be misunderstanding what that actually means...

Octave's current behavior is compatible to Matlab's.

Markus


Reply | Threaded
Open this post in threaded view
|

Re: Synonym

John W. Eaton
Administrator
In reply to this post by Tony Richardson-4
On 4/8/20 12:59 PM, Tony Richardson wrote:

> 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.

I pushed the following changeset to fix that problem:

   http://hg.savannah.gnu.org/hgweb/octave/rev/56c209ff0a08

jwe


Reply | Threaded
Open this post in threaded view
|

Re: Synonym

Thomas D. Dean-2
On 2020-04-08 12:02, John W. Eaton wrote:

> On 4/8/20 12:59 PM, Tony Richardson wrote:
>
>> 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.
>
> I pushed the following changeset to fix that problem:
>
>    http://hg.savannah.gnu.org/hgweb/octave/rev/56c209ff0a08
>
> jwe
>
>

This seems to fail.

octave:20> version("-hgid")
ans = 7ac8b3188b27+
octave:21> function varargout = xsvd (varargin)
 >   numel (varargin)
 >   [varargout{:}] = svd (varargin{:});
 > end
octave:22> xsvd (rand (2))
ans = 1
error: invalid dimension inquiry of a non-existent value
error: called from
     xsvd at line 3 column 18

Or, did I do something wrong?

Tom Dean


Reply | Threaded
Open this post in threaded view
|

Re: Synonym

siko1056
On 4/9/20 11:52 AM, Thomas D. Dean wrote:

> On 2020-04-08 12:02, John W. Eaton wrote:
>> On 4/8/20 12:59 PM, Tony Richardson wrote:
>>
>>> I've noticed that the two cases:
>>>
>>>     varargout = cell (nargout, 1);
>>>     [varargout{:}] = svd(varargin{:});
>>>
>>> and
>>>     [varargout{1:nargout}] = svd(varargin{:});
>>>
> [...]
>
> This seems to fail.
>
> octave:21> function varargout = xsvd (varargin)
>>   numel (varargin)
>>   [varargout{:}] = svd (varargin{:});
>> end
>
> [...]
>
> Tom Dean
>

Please watch carefully at the two cases above, you unfortunately mixed
them up.

  function varargout = xsvd (varargin)
    [varargout{1:nargout}] = svd(varargin{:});
  endfunction

HTH,
Kai


Reply | Threaded
Open this post in threaded view
|

Re: Synonym

Thomas D. Dean-2
On 2020-04-08 21:26, Kai Torben Ohlhus wrote:
> function varargout = xsvd (varargin)
>      [varargout{1:nargout}] = svd(varargin{:});
>    endfunction

Thanks.

Tom Dean