Path: blob/master/Week 9/Programming Assignment - 8/ex8/lib/jsonlab/varargin2struct.m
626 views
function opt=varargin2struct(varargin)1%2% opt=varargin2struct('param1',value1,'param2',value2,...)3% or4% opt=varargin2struct(...,optstruct,...)5%6% convert a series of input parameters into a structure7%8% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)9% date: 2012/12/2210%11% input:12% 'param', value: the input parameters should be pairs of a string and a value13% optstruct: if a parameter is a struct, the fields will be merged to the output struct14%15% output:16% opt: a struct where opt.param1=value1, opt.param2=value2 ...17%18% license:19% BSD, see LICENSE_BSD.txt files for details20%21% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)22%2324len=length(varargin);25opt=struct;26if(len==0) return; end27i=1;28while(i<=len)29if(isstruct(varargin{i}))30opt=mergestruct(opt,varargin{i});31elseif(ischar(varargin{i}) && i<len)32opt=setfield(opt,varargin{i},varargin{i+1});33i=i+1;34else35error('input must be in the form of ...,''name'',value,... pairs or structs');36end37i=i+1;38end39404142