help-octave
[Top][All Lists]
Advanced

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

Re: Arguments out?


From: Paul Kienzle
Subject: Re: Arguments out?
Date: Fri, 12 Dec 2003 19:18:54 -0500

Nothing a little testing before posting won't fix ;-)

The biggest problem was not using brackets in
"function [varargout]".  I'm not sure if this is an octave bug.

The second problem was not using a cell array for case 0,1.

Try this:

function [varargout] = test (x, y, z)
  switch nargout
    case {0, 1}
      varargout = {z};

    case 2
      varargout = {z, x};

    case 3
      varargout = {z, y, x};

    otherwise
      usage ("oops");

  endswitch;
endfunction


Paul Kienzle
address@hidden


On Dec 12, 2003, at 10:03 AM, Vic Norton wrote:

Hi Paul,

The second method would work, but the first one is rather confusing. For example I tried

function varargout = test(x, y, z)
        switch nargout
                case {0,1} varargout = z;
                case 2, varargout = { z, x };
                case 3, varargout = { z, y, x };
                otherwise, usage("z = test(x, y, z) or ...");
        endswitch
endfunction

on three matrices, A, B, and C, of different sizes. Here is what came out

test(A, B, C)  returned ans = C
z = test(A, B, C)  returned z = C

OK so far, but

[z, x] = test(A, B, C) returned

   z =
   {
     [1,1] = C

     [1,2] = A
   }

and the message

   error: element number 2 undefined in return list

For my application I think I just going to leave things in the order

   function  [x, pi, fail] = simp(A, b, c, I)

Then the function will naturally produce

   a solution
   a solution and the dual multiplier
   a solution, the dual multiplier, and failure information

depending on nargout.

Thanks for your comments!

Regards,

Vic

At 9:31 PM -0500 12/11/03, Paul Kienzle wrote:

So write it using varargout:

function varargout = svd(A)
    ...
    switch nargout
      {0,1}, varargout = { diag(s) };
      3, varargout = { u, s, v };
      otherwise, usage("sigma=svd(A) or [u,s,v]=svd(A)");
   end
end

or equivalently using meaningless placeholders
for the returned values:

function [r1,r2,r3] = mysvd(A)
  ...
  if nargout<=1, r1=diag(s);
  else r1=u; r2=s; r3=v; end
end

Paul Kienzle
address@hidden





-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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