DefaultArgs.m 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. function varargout = DefaultArgs(Args, DefArgs)
  2. % auxillary function to replace argument check in the beginning and def. args assigment
  3. % sets the absent or empty values of the Args (cell array, usually varargin)
  4. % to their default values from the cell array DefArgs.
  5. % Output should contain the actuall names of arguments that you use in the function
  6. % e.g. : in function MyFunction(somearguments , varargin)
  7. % calling [SampleRate, BinSize] = DefaultArgs(varargin, {20000, 20});
  8. % will assign the defualt values to SampleRate and BinSize arguments if they
  9. % are empty or absent in the varargin cell list
  10. % (not passed to a function or passed empty)
  11. if isempty(Args)
  12. Args ={[]};
  13. end
  14. % if iscell(Args) & isstr(Args{1}) & length(Args)==1
  15. % Args = Args{1};
  16. % end
  17. if ~iscell(DefArgs)
  18. DefArgs = {DefArgs};
  19. end
  20. nDefArgs = length(DefArgs);
  21. nInArgs = length(Args);
  22. %out = cell(nDefArgs,1);
  23. if (nargout~=nDefArgs)
  24. error('number of defaults is different from assigned');
  25. %keyboard
  26. end
  27. for i=1:nDefArgs
  28. if (i>nInArgs | isempty(Args{i}))
  29. varargout(i) = {DefArgs{i}};
  30. else
  31. varargout(i) = {Args{i}};
  32. end
  33. end