Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
varargin (no name list) vs. if (has name list)

Subject: varargin (no name list) vs. if (has name list)

From: Mike

Date: 2 Aug, 2013 01:59:20

Message: 1 of 4

Hi

if one uses varargin, like
   function varlist2(X,Y,varargin)
one doesn't know what name the varargin really is.

I try:
function varlist2(X,Y)
fprintf('%d \n',X);
if(isequal(X,2))
    fprintf('%d \n',Y);
end
end
then
>> varlist2(1)
1
>> varlist2(2)
2
??? Input argument "Y" is undefined.

Error in ==> varlist2 at 4
    fprintf('%d \n',Y);
 
>> varlist2(2,5)
2
5

It works and also has varargin: Y and also have a good name.
Isn't this better?

Mike

Subject: varargin (no name list) vs. if (has name list)

From: Kevin Hellemans

Date: 2 Aug, 2013 07:21:11

Message: 2 of 4

Your question isn't entirely clear. Do you want to print Y when you have two input arguments or only when X has value 2? For the former this might work:

function varlist2(X,varargin)
narginchk(1,2)
switch nargin
    case 1
        fprintf('%d \n',X);
    case 2
        Y = varargin{1};
        fprintf('%d \n',X);
        fprintf('%d \n',Y);
end
end

For the latter you should be carefull not to try to print Y if only one input argument is given, so something like:

function varlist2(X,varargin)
narginchk(1,2)
fprintf('%d \n',X);
if X==2 & nargin == 2
        Y = varargin{1};
        fprintf('%d \n',Y);
end
end

Subject: varargin (no name list) vs. if (has name list)

From: dpb

Date: 2 Aug, 2013 12:51:47

Message: 3 of 4

On 8/1/2013 8:59 PM, Mike wrote:
> Hi
>
> if one uses varargin, like
> function varlist2(X,Y,varargin)
> one doesn't know what name the varargin really is.
>
...

Yes one does--it's "varargin" and is a cell array containing the
remaining actual arguments in the third and subsequent positions in the
calling statement, _if_(and_only_if)_ present.

Your code is in error because it assumes there is a value for Y in all
cases even when it isn't there as well as varargin being empty.

You _MUST_ check the number of arguments actually passed with nargin and
ensure your function never references ones that don't exist in any given
call.

Rightfully, any time you write a function w/ two dummy arguments as
above you should require them both to be present and error if not. For
quick code one often neglects such niceties but by doing so one gets bad
results as you did...

--

Subject: varargin (no name list) vs. if (has name list)

From: Steven_Lord

Date: 2 Aug, 2013 14:02:39

Message: 4 of 4



"Mike" <SulfateIon@gmail.com> wrote in message
news:15e26e51-abfd-45bd-a8bd-b2529e251b33@googlegroups.com...
> Hi
>
> if one uses varargin, like
> function varlist2(X,Y,varargin)
> one doesn't know what name the varargin really is.

What name the varargin really is WHERE? In the workspace from which varlist2
is being called? There's no guarantee the values you pass into varlist2 as
the 3rd, 4th, etc. inputs HAVE names in that workspace.

x = 4;
varlist2(1, 2, 3, x, x+1, 6)

3 has no name; it's just 3. The second element of varargin (aka the 4th
input to varlist2) has a name in the caller workspace; its name is x. The
third element of varargin (aka the 5th element to varlist2) also has no
name, since x+1 is an _expression_ not a variable.

> I try:
> function varlist2(X,Y)
> fprintf('%d \n',X);
> if(isequal(X,2))
> fprintf('%d \n',Y);
> end
> end
> then
>>> varlist2(1)
> 1
>>> varlist2(2)
> 2
> ??? Input argument "Y" is undefined.
>
> Error in ==> varlist2 at 4
> fprintf('%d \n',Y);
>
>>> varlist2(2,5)
> 2
> 5
>
> It works and also has varargin: Y and also have a good name.
> Isn't this better?

Better than what? Better than this?

function varlist2(X,varargin)
fprintf('%d \n',X);
if(isequal(X,2))
   fprintf('%d \n',varargin{1});
end
end

>> varlist2(1)
1
>> varlist2(2)
2
Index exceeds matrix dimensions.

Error in varlist2 (line 4)
   fprintf('%d \n',varargin{1});


If the second input argument is truly optional, make sure that your code
handles both the case where it is present and the case where it is not.

function varlist2(X,varargin)
fprintf('%d \n',X);
if(isequal(X,2))
    if nargin > 1
        fprintf('%d \n',varargin{1});
    else
        error('VARLIST2 expects to be called with 2 inputs when the first
input is 2.');
    end
end
end

>> varlist2(1)
1
>> varlist2(2)
2
Error using varlist2 (line 7)
VARLIST2 expects to be called with 2 inputs when the first input is 2.

>> varlist2(2, 3)
2
3

Sure, it requires more coding. But it's not much more coding, and YOU get to
control the exact information that your user sees, which means you may be
able to give them enough information so that they know what they did that
they shouldn't have or vice versa. In either of the first two cases, a user
(particularly a novice user) may not know exactly how to resolve the
problem. IMO the third case tells the user exactly what they need to do:
call VARLIST2 with 2 inputs if they specify 2 as the first input.
Alternately instead of throwing an ERROR you could use WARNING to tell them
what to do next time and what you're going to do.

function varlist2(X,varargin)
fprintf('%d \n',X);
if(isequal(X,2))
    if nargin == 1
        warning('Specify two input arguments when you call VARLIST2 with a
first input of 2. Using a default of 0.')
        Y = 0;
    else
        Y = varargin{1};
    end
    fprintf('%d \n',Y);
end
end

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

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.

Contact us