opt=varargin2struct('param1',value1,'param2',value2,...) or opt=varargin2struct(...,optstruct,...) convert a series of input parameters into a structure authors:Qianqian Fang (q.fang <at> neu.edu) date: 2012/12/22 input: 'param', value: the input parameters should be pairs of a string and a value optstruct: if a parameter is a struct, the fields will be merged to the output struct output: opt: a struct where opt.param1=value1, opt.param2=value2 ... license: BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
0001 function opt=varargin2struct(varargin) 0002 % 0003 % opt=varargin2struct('param1',value1,'param2',value2,...) 0004 % or 0005 % opt=varargin2struct(...,optstruct,...) 0006 % 0007 % convert a series of input parameters into a structure 0008 % 0009 % authors:Qianqian Fang (q.fang <at> neu.edu) 0010 % date: 2012/12/22 0011 % 0012 % input: 0013 % 'param', value: the input parameters should be pairs of a string and a value 0014 % optstruct: if a parameter is a struct, the fields will be merged to the output struct 0015 % 0016 % output: 0017 % opt: a struct where opt.param1=value1, opt.param2=value2 ... 0018 % 0019 % license: 0020 % BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 0021 % 0022 % -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) 0023 % 0024 0025 len=length(varargin); 0026 opt=struct; 0027 if(len==0) return; end 0028 i=1; 0029 while(i<=len) 0030 if(isstruct(varargin{i})) 0031 opt=mergestruct(opt,varargin{i}); 0032 elseif(ischar(varargin{i}) && i<len) 0033 opt=setfield(opt,lower(varargin{i}),varargin{i+1}); 0034 i=i+1; 0035 else 0036 error('input must be in the form of ...,''name'',value,... pairs or structs'); 0037 end 0038 i=i+1; 0039 end 0040