Thread Subject: varargin - Best Practices?

Subject: varargin - Best Practices?

From: neuromath

Date: 18 Jan, 2008 23:23:36

Message: 1 of 3

I'd like to get some input on 'varargin', which I've come across
recently and have been using to add utility to various functions
depending on the number of things I choose to specify. For instance,
let's say I have a function:

function foo(varargin)
     NArg = size(varargin,2);

     if NArg == 1
          x = varargin{1};
     elseif NArg == 2
          y = varargin{1};
          z = varargin{2};
     else
          do something fancy;
     end

     perform some action using either x or y,z;
end

I'm sort of "overloading" that first input by letting it either be
some 'x' or some 'y'. Does anyone have any opinion on whether or not
this is a reasonable practice? Are there any potential pitfalls (aside
from poor documentation and misuse)? Input appreciated ....

Subject: varargin - Best Practices?

From: dpb

Date: 19 Jan, 2008 01:27:54

Message: 2 of 3

neuromath wrote:
> I'd like to get some input on 'varargin', which I've come across
> recently and have been using to add utility to various functions
> depending on the number of things I choose to specify. For instance,
> let's say I have a function:
>
> function foo(varargin)
> NArg = size(varargin,2);
>
> if NArg == 1
> x = varargin{1};
> elseif NArg == 2
> y = varargin{1};
> z = varargin{2};
> else
> do something fancy;
> end
>
> perform some action using either x or y,z;
> end
>
> I'm sort of "overloading" that first input by letting it either be
> some 'x' or some 'y'. Does anyone have any opinion on whether or not
> this is a reasonable practice? Are there any potential pitfalls (aside
> from poor documentation and misuse)? Input appreciated ....

Those would seem significant ones to me... :)

Why would you put such seemingly unrelated functions together in the
first place is the first question that comes to my mind...

--

Subject: varargin - Best Practices?

From: James Tursa

Date: 19 Jan, 2008 01:42:02

Message: 3 of 3

> NArg = size(varargin,2);

Why are you doing this vs just using the supplied nargin
variable?

James Tursa

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com