varargin2struct.m 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. function opt=varargin2struct(varargin)
  2. %
  3. % opt=varargin2struct('param1',value1,'param2',value2,...)
  4. % or
  5. % opt=varargin2struct(...,optstruct,...)
  6. %
  7. % convert a series of input parameters into a structure
  8. %
  9. % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
  10. % date: 2012/12/22
  11. %
  12. % input:
  13. % 'param', value: the input parameters should be pairs of a string and a value
  14. % optstruct: if a parameter is a struct, the fields will be merged to the output struct
  15. %
  16. % output:
  17. % opt: a struct where opt.param1=value1, opt.param2=value2 ...
  18. %
  19. % license:
  20. % BSD, see LICENSE_BSD.txt files for details
  21. %
  22. % -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
  23. %
  24. len=length(varargin);
  25. opt=struct;
  26. if(len==0) return; end
  27. i=1;
  28. while(i<=len)
  29. if(isstruct(varargin{i}))
  30. opt=mergestruct(opt,varargin{i});
  31. elseif(ischar(varargin{i}) && i<len)
  32. opt=setfield(opt,varargin{i},varargin{i+1});
  33. i=i+1;
  34. else
  35. error('input must be in the form of ...,''name'',value,... pairs or structs');
  36. end
  37. i=i+1;
  38. end