findarg

PURPOSE ^

FINDARG - find argument associated to a field in a list

SYNOPSIS ^

function vals=findarg(arglist,field)

DESCRIPTION ^

FINDARG - find argument associated to a field in a list

   This function parses through an argument list (typically varargin in a routine)
   looking for a character array equal to field. Once this is found, we return the 
   next value in the varargin (if possible). 
   Because field might appear several times in the argument list, we return a structure 
   holding all these values. 
   Note that all comparisons to field value are case independent.

   Usage:
      vals=findarg(arglist,field)

   Example:
      routine foobar calls vals=findarg('Data',varargin)
      with varargin='Data',1,'Data','foo','Plot','velocity','Arrow',4
      findarg would return the following structure: vals(1).value=1, vals(2).value='foo';

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function  vals=findarg(arglist,field)
0002 %FINDARG - find argument associated to a field in a list
0003 %
0004 %   This function parses through an argument list (typically varargin in a routine)
0005 %   looking for a character array equal to field. Once this is found, we return the
0006 %   next value in the varargin (if possible).
0007 %   Because field might appear several times in the argument list, we return a structure
0008 %   holding all these values.
0009 %   Note that all comparisons to field value are case independent.
0010 %
0011 %   Usage:
0012 %      vals=findarg(arglist,field)
0013 %
0014 %   Example:
0015 %      routine foobar calls vals=findarg('Data',varargin)
0016 %      with varargin='Data',1,'Data','foo','Plot','velocity','Arrow',4
0017 %      findarg would return the following structure: vals(1).value=1, vals(2).value='foo';
0018 
0019 %some argument checking:
0020 if ((nargin==0) | (nargout==0)),
0021     help findarg;
0022     error('findarg error message');
0023 end
0024 
0025 if ~ischar(field),
0026     error('findarg error message: field should be a string');
0027 end
0028 
0029 if ~iscell(arglist),
0030     error('findarg error message: argument list should be a cell array.');
0031 end
0032 
0033 %Recover data to plot
0034 founddata=0;
0035 
0036 for i=1:(length(arglist)-1), %data in arglist comes in pairs, hence the -1.
0037     if ischar(arglist{i}),
0038         if (strcmpi(arglist{i},field)),
0039             founddata=founddata+1;
0040             if founddata==1,
0041                 vals.value=arglist{i+1};
0042             else
0043                 vals(end+1).value=arglist{i+1};
0044             end
0045             i=i+1;
0046         end
0047     end
0048 end
0049 
0050 if founddata==0,
0051     vals=[];
0052 end

Generated on Sun 29-Mar-2009 20:22:55 by m2html © 2003