Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

overridedefaults.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. %parseargs [opts] = overridedefaults(varnames, arguments, ignore_unknowns)
  2. %
  3. % Variable argument parsing
  4. %
  5. % function is meant to be used in the context of other functions
  6. % which have variable arguments. Typically, the function using
  7. % variable argument parsing would be written with the following
  8. % header:
  9. %
  10. % function myfunction(args, ..., varargin)
  11. %
  12. % and would set up some default values for variables:
  13. % bin_size = 0.1;
  14. % thresh = 2;
  15. %
  16. % overridedefaults(whos, varargin);
  17. %
  18. %
  19. % varargin can be of two forms:
  20. % 1) A cell array where odd entries are variable names and even entries are
  21. % the corresponding values
  22. % 2) A struct where the fieldnames are the variable names and the values of
  23. % the fields are the values (for pairs)
  24. %
  25. %
  26. % OVERRIDEDEFAULTS USES ASSIGNIN COMMANDS TO CHANGE OR SET VALUES OF
  27. % VARIABLES IN THE CALLING FUNCTION'S SPACE! THE RETURN VALUES ARE INTENDED
  28. % FOR ACCOUNTING PURPOSES ONLY AND USE OF THEM IS NOT CRITICAL FOR THE
  29. % CODE'S PRIMARY FUNCTIONALITY.
  30. %
  31. % OUTPUT:
  32. %
  33. % opts: Optional output that is a variable with fields corresponding to
  34. % each varname passed in or newly created varname. Values of
  35. % those fields are the final assigned value of the corresponding
  36. % varname variable.
  37. %
  38. % PARAMETERS:
  39. % -----------
  40. % -varnames The list of variable names which are defined in the
  41. % calling function
  42. % -arguments The varargin list, I.e. a row cell array.
  43. % value for the variable.
  44. %
  45. %
  46. % Example:
  47. % --------
  48. % Let's say i have a function foo
  49. % function foo(x,varargin)
  50. % a = 5;
  51. % b= 1;
  52. % overridedefaults(whos, varargin);
  53. %
  54. % Then in the workspace I call:
  55. % foo(100,'a',1,'b',2)
  56. %
  57. % This will override the values of a and b to be 1 and 2.
  58. %
  59. % Note that the arguments to `foo` may be in any order-- the
  60. % only ordering restriction is that whatever immediately follows
  61. % pair names (e.g. 'a') will be interpreted as the value to be
  62. % assigned to them (e.g. 'a' takes on the value 1);
  63. %
  64. % We encourage the use of utils.inputordefault over this function.
  65. %
  66. function [varargout] = overridedefaults(varnames, arguments, ignore_unknowns)
  67. if nargin < 3, ignore_unknowns=true; end;
  68. if nargout>0
  69. % to start, assign the default values to the optional output
  70. for v=1:numel(varnames)
  71. out.(varnames{v}) = evalin('caller',varnames{v});
  72. end
  73. end
  74. % Now we assign the value to those passed by arguments.
  75. if numel(arguments)==1 && isstruct(arguments{1})
  76. arguments=arguments{1};
  77. fn=fieldnames(arguments);
  78. for arg=1:numel(fn)
  79. switch fn{arg}
  80. case varnames
  81. assignin('caller',fn{arg}, arguments.(fn{arg}));
  82. out.(fn{arg})=arguments.(fn{arg});
  83. otherwise
  84. if ignore_unknowns
  85. warning('OD:unknown','Variable %s not defined in caller. Skipping.',fn{arg});
  86. else
  87. assignin('caller',fn{arg}, arguments.(fn{arg}));
  88. end
  89. end
  90. end
  91. else
  92. arg = 1;
  93. while arg <= length(arguments),
  94. switch arguments{arg},
  95. case varnames,
  96. if arg+1 <= length(arguments)
  97. assignin('caller', arguments{arg}, arguments{arg+1});
  98. out.(arguments{arg})=arguments{arg+1};
  99. arg = arg+1;
  100. end;
  101. otherwise
  102. if ignore_unknowns
  103. warning('OD:unknown','Variable %s not defined in caller. Skipping.',arguments{arg});
  104. else
  105. assignin('caller', arguments{arg}, arguments{arg+1});
  106. out.(arguments{arg})=arguments{arg+1};
  107. end
  108. arg = arg+1;
  109. end;
  110. arg = arg+1;
  111. end;
  112. end
  113. if nargout>0
  114. varargout{1}=out;
  115. end
  116. return;