% options - create name-value map as a structure
%
% Arguments can be name value pairs, eg
%   opts=options('foo',5,'bar',7)
%   opts.foo=4
%   opts.bar=7
%
% Or structures
%   opts=options(defopts,'foo',5)
%
% Later arguments override earlier ones. A typical usage is 
% with variable argument lists and GETPARAM, eg
%
%   function somefun(a,b,varargin)
%   ...
%   opts=options('foo',5,varargin{:});
%   fooness=opts.foo;
%   barness=getparam(opts,'bar',12);
%
% See also: GETPARAM, GETFIELD, ISFIELD
function P=options(varargin)

P=struct;
n=1;
while n<=length(varargin)
	arg=varargin{n};
	if isstruct(arg)
		P=cpfields(fieldnames(arg),arg,P); 
	elseif iscell(arg)	
		pairs=arg;
		for k=1:length(pairs)
			P=setfield(P,pairs{k}{1},pairs{k}{2});
		end
	else
		P=setfield(P,varargin{n},varargin{n+1});
		n=n+1;
	end
	n=n+1;
end

