Thread Subject: Proper use of varagin with parameter-value pairs

Subject: Proper use of varagin with parameter-value pairs

From: Daniel Ennis

Date: 8 Oct, 2004 14:37:14

Message: 1 of 5

I have a method that I use to handle parameter-value pairs as inputs
to a function, but everytime I code it it seems inefficient. Is
there a better way?

The main "problem" is that I declare the all potential varargin
inputs to be empty so that I can test to see if they are *still*
empty after the varargin inputs have been parsed. If its still empty
then I apply a default value. It seems like there might be a better
construct for setting a default value if a value is not included in
the varargin list. Ideas?

Sample code snippet:

% Lets say a function requires the inputs
% A, B, C, but a variety of
% parameter-value pairs can follow.
%
% Typical input:
% junk(1,2,3,'color',[1 0 0]);
% OR
% junk(1,2,3,'temp',12,'color',[1 1 0]);

function junk(A,B,C,varargin)

% Possible inputs to varargin
color=[];
temp=[];
position=[];

% Switch trap parses the varargin inputs
i=1;
while i <= length(varargin)
  switch lower(varargin{i})
    case 'color'
      color=varargin{i+1}; i=i+2;
    case 'temp'
      temp=varargin{i+1}; i=i+2;
    case 'position'
      position=varargin{i+1}; i=i+2;
    otherwise
      error('Unknown option: %s\n',varargin{i}); i=i+1;
  end
end

% If the variables remains empty then
% define a default value...
if isempty(color), color=[0.5 1 1]; end
if isempty(temp), temp =10; end
if isempty(position), position=11; end

% Rest of program....

return

Subject: Proper use of varagin with parameter-value pairs

From: Daniel Ennis

Date: 8 Oct, 2004 14:43:34

Message: 2 of 5

So I found the exist.m function. That seems to help. So now the
function would look like the following. Is this good form?

function junk(A,B,C,varargin)

% Switch trap parses the varargin inputs
i=1;
while i <= length(varargin)
  switch lower(varargin{i})
    case 'kolor'
      kolor=varargin{i+1}; i=i+2;
    case 'temp'
      temp=varargin{i+1}; i=i+2;
    case 'position'
      position=varargin{i+1}; i=i+2;
    otherwise
      error('Unknown option: %s\n',varargin{i}); i=i+1;
  end
end

% If the variables does not exist
% define a default value
if ~exist('kolor'), kolor=[0.5 1 1]; end
if ~exist('temp'), temp =10; end
if ~exist('position'), position=11; end

% Rest of program....

return

Subject: Proper use of varagin with parameter-value pairs

From: per isakson

Date: 8 Oct, 2004 15:32:10

Message: 3 of 5

Daniel Ennis wrote:
>
>
> I have a method that I use to handle parameter-value pairs as
> inputs
> to a function, but everytime I code it it seems inefficient. Is
> there a better way?
>
> The main "problem" is that I declare the all potential varargin
> inputs to be empty so that I can test to see if they are *still*
> empty after the varargin inputs have been parsed. If its still
> empty
> then I apply a default value. It seems like there might be a
> better
> construct for setting a default value if a value is not included in
> the varargin list. Ideas?
>

Here is a code fragment that shows how I do it

oh = varargin{ 1 };
if isEven( nargin - 1 ),
    caNames = transpose( varargin( 2 : 2 : end ) );
    caValues = transpose( varargin( 3 : 2 : end ) );
    A = cell2struct( caValues, caNames, 1 );
else,
    error( ['poi: '] ),
end

A = MergeStructs( A, Defaults );

MergeStructs adds default properties, takes care of truncated Names
and changes Upper/Lower case in the Names to agree with the Defaults.
Thus, I mimic the handle graphic convention.

/ per

Subject: Proper use of varagin with parameter-value pairs

From: Brett Shoelson

Date: 8 Oct, 2004 15:36:11

Message: 4 of 5


"Daniel Ennis" <dbe@stanford.edu> wrote in message
news:eeec7b6.-1@webx.raydaftYaTP...
>I have a method that I use to handle parameter-value pairs as inputs
> to a function, but everytime I code it it seems inefficient. Is
> there a better way?
>
> The main "problem" is that I declare the all potential varargin
> inputs to be empty so that I can test to see if they are *still*
> empty after the varargin inputs have been parsed. If its still empty
> then I apply a default value. It seems like there might be a better
> construct for setting a default value if a value is not included in
> the varargin list. Ideas?
>
> Sample code snippet:
>
> % Lets say a function requires the inputs
> % A, B, C, but a variety of
> % parameter-value pairs can follow.
> %
> % Typical input:
> % junk(1,2,3,'color',[1 0 0]);
> % OR
> % junk(1,2,3,'temp',12,'color',[1 1 0]);
>
> function junk(A,B,C,varargin)
>
> % Possible inputs to varargin
> color=[];
> temp=[];
> position=[];
>
> % Switch trap parses the varargin inputs
> i=1;
> while i <= length(varargin)
> switch lower(varargin{i})
> case 'color'
> color=varargin{i+1}; i=i+2;
> case 'temp'
> temp=varargin{i+1}; i=i+2;
> case 'position'
> position=varargin{i+1}; i=i+2;
> otherwise
> error('Unknown option: %s\n',varargin{i}); i=i+1;
> end
> end
>
> % If the variables remains empty then
> % define a default value...
> if isempty(color), color=[0.5 1 1]; end
> if isempty(temp), temp =10; end
> if isempty(position), position=11; end
>
> % Rest of program....
>
> return

Hi Daniel,
There's nothing inherently "wrong" with your approach, but it makes for
inefficient code...more work on your part to list and account for all
posible inputs. However, if you don't mind using an (evil) eval statement,
you might consider something more along the lines of :

for ii = 1:2:length(varargin)
   eval(sprintf('%s = %s',varargin{ii},varargin{ii+1});
end

(Note that this simplistic strfun requires inputs in string form. Shouldn't
be too difficult to modify that to accept whatever form you want to use.)
It's also likely not too difficult to use a similar approach without that
eliminates the eval.
Also, instead of initializing your variables to []'s and then using if-thens
to set them if they remain empty, why not simply initialize them to the
defaults, and overwrite them if the input is entered?
Cheers,
Brett

Note that you might need an eval statement, given your inpu

Subject: Proper use of varagin with parameter-value pai

From: Martin Rickli

Date: 11 Oct, 2004 05:58:43

Message: 5 of 5

Daniel Ennis wrote:
>
>
> I have a method that I use to handle parameter-value pairs as
> inputs
> to a function, but everytime I code it it seems inefficient. Is
> there a better way?
>
> The main "problem" is that I declare the all potential varargin
> inputs to be empty so that I can test to see if they are *still*
> empty after the varargin inputs have been parsed. If its still
> empty
> then I apply a default value. It seems like there might be a
> better
> construct for setting a default value if a value is not included in
> the varargin list. Ideas?
>
> Sample code snippet:
>
> % Lets say a function requires the inputs
> % A, B, C, but a variety of
> % parameter-value pairs can follow.
> %
> % Typical input:
> % junk(1,2,3,'color',[1 0 0]);
> % OR
> % junk(1,2,3,'temp',12,'color',[1 1 0]);
>
> function junk(A,B,C,varargin)
>
> % Possible inputs to varargin
> color=[];
> temp=[];
> position=[];
>
> % Switch trap parses the varargin inputs
> i=1;
> while i <= length(varargin)
> switch lower(varargin{i})
> case 'color'
> color=varargin{i+1}; i=i+2;
> case 'temp'
> temp=varargin{i+1}; i=i+2;
> case 'position'
> position=varargin{i+1}; i=i+2;
> otherwise
> error('Unknown option: %s\n',varargin{i}); i=i+1;
> end
> end
>
> % If the variables remains empty then
> % define a default value...
> if isempty(color), color=[0.5 1 1]; end
> if isempty(temp), temp =10; end
> if isempty(position), position=11; end
>
> % Rest of program....
>
> return

Forget your empty settings!
Instead define your default values first. Then in the switch-case you
overwrite the defaults with user values.

Hope this helps
Regards Martin

% example code -------------------------
function junk(A,B,C,varargin)

% Default settings
color = [0.5 1 1];
temp = 10;
position = 11;

% Switch trap parses the varargin inputs
len = length(varargin);
% check "len" for even number
if mod(len,2) > 0
    error('Wrong arguments: must be name-value pairs.');
end
for i = 1:2:len
    switch lower(varargin{i})
        case 'color'
            color=varargin{i+1};
        case 'temp'
            temp=varargin{i+1};
        case 'position'
            position=varargin{i+1};
        otherwise
            % neglect invalid option
    end
end

% rest of code

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