indsample.m 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. function res = indsample(this, t)
  2. % Method for getting the sample closest to some time point
  3. % FORMAT res = indsample(this, t)
  4. % this - MEEG object
  5. % t - vector of time points in seconds
  6. %
  7. % res - vector of sample indices matching time points
  8. %__________________________________________________________________________
  9. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  10. % Stefan Kiebel
  11. % $Id: indsample.m 3742 2010-03-02 15:15:43Z vladimir $
  12. res = NaN(1,length(t));
  13. if this.Nsamples > 0
  14. T = time(this);
  15. for i = 1:length(t)
  16. if isfinite(t(i))
  17. [m,res(i)] = min(abs(T-t(i)));
  18. if m > (1/this.Fsample)
  19. warning('Could not find an index matching the requested time %d sec', t(i));
  20. res(i) = NaN;
  21. end
  22. elseif ~isnan(t(i)) % This allows to specify the time window as [-Inf Inf]
  23. if t(i) < 0
  24. res(i) = 1;
  25. else
  26. res(i) = this.Nsamples;
  27. end
  28. end
  29. end
  30. end