spm_funfun.m 849 B

1234567891011121314151617181920212223242526272829
  1. function [x] = spm_funfun(varargin)
  2. % Utility function to evaluate functionals
  3. % FORMAT [F] = spm_funfun({f1,x11,x12,..f2,x22,...)
  4. %
  5. % F = f ... f2(f1(x11,x12,...),x22,...)) ... )
  6. %
  7. % e.g. spm_funfun(@(x) cos(x),2.1,@(x,a) x^a,2)
  8. %
  9. % which is cos(2.1)^2
  10. %__________________________________________________________________________
  11. % Copyright (C) 2005-2013 Wellcome Trust Centre for Neuroimaging
  12. % Karl Friston
  13. % $Id: spm_funfun.m 5219 2013-01-29 17:07:07Z spm $
  14. % iterate over functions
  15. %--------------------------------------------------------------------------
  16. while ~isempty(varargin)
  17. f = varargin{1};
  18. n = nargin(f);
  19. try
  20. x = {x,varargin{2:n}};
  21. varargin = varargin(n + 1:end);
  22. catch
  23. x = varargin(2:(n + 1));
  24. varargin = varargin(n + 2:end);
  25. end
  26. x = feval(f,x{:});
  27. end