extractdatapt.m 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. function data=extractdatapt(data,t,offset)
  2. % Extract segements of spike times between t(1) and t(2)
  3. % Usage: data=extractdatapt(data,t,offset)
  4. %
  5. % Input:
  6. % data: structural array of spike times for each channel/trial or a single
  7. % array of spike times
  8. % t : time as a 2d vector [t(1) t(2)]
  9. % offset: 0/1 - if 1, store the spike times relative to start of window i.e. t(1)
  10. % if 0, don't reset the times. Default 0.
  11. % Note that all times can be in arbitrary units. But the units have to be
  12. % consistent. So, if E is in secs, win, t have to be in secs, and Fs has to
  13. % be Hz. If E is in samples, so are win and t, and Fs=1. In case of spike
  14. % times, the units have to be consistent with the units of data as well.
  15. %
  16. % Output:
  17. % data: spike times between t(1) and t(2)
  18. if nargin < 2; error('Need data and times'); end;
  19. if t(1) < 0 || t(2)<=t(1);
  20. error('times cannot be negative and t(2) has to greater than t(1)');
  21. end;
  22. if nargin < 3 || isempty(offset); offset=0; end;
  23. if isstruct(data);
  24. C=length(data);
  25. elseif min(size(data))~=1;
  26. error('Can only accept single vector data unless it is a struct array');
  27. else
  28. C=1;
  29. data=change_row_to_column(data);
  30. end;
  31. %fnames=fieldnames(data);
  32. d2(1:C)=struct('times',[]);
  33. for c=1:C,
  34. if isstruct(data)
  35. fnames=fieldnames(data);
  36. eval(['dtmp=data(c).' fnames{1} ';'])
  37. else
  38. dtmp=data(:);
  39. end
  40. % eval(['dtmp=data(c).' fnames{1} ';' ])
  41. sp=dtmp(dtmp>=t(1) & dtmp<t(2));
  42. if offset==1; d2(c).times=sp-t(1);
  43. else d2(c).times=sp;end
  44. end;
  45. data=d2;