jsonopt.m 881 B

1234567891011121314151617181920212223242526272829303132
  1. function val=jsonopt(key,default,varargin)
  2. %
  3. % val=jsonopt(key,default,optstruct)
  4. %
  5. % setting options based on a struct. The struct can be produced
  6. % by varargin2struct from a list of 'param','value' pairs
  7. %
  8. % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
  9. %
  10. % $Id: loadjson.m 371 2012-06-20 12:43:06Z fangq $
  11. %
  12. % input:
  13. % key: a string with which one look up a value from a struct
  14. % default: if the key does not exist, return default
  15. % optstruct: a struct where each sub-field is a key
  16. %
  17. % output:
  18. % val: if key exists, val=optstruct.key; otherwise val=default
  19. %
  20. % license:
  21. % BSD, see LICENSE_BSD.txt files for details
  22. %
  23. % -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
  24. %
  25. val=default;
  26. if(nargin<=2) return; end
  27. opt=varargin{1};
  28. if(isstruct(opt) && isfield(opt,key))
  29. val=getfield(opt,key);
  30. end