plotRaster_pub.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. function plotRaster_pub(myTimes, timeLims)
  2. %function [sdfs, sdfTimes, allAxH] = plotRaster_pub(myTimes, timeLims)
  3. % PLOTRASTER_PUB makes raster plot
  4. %
  5. % PLOTRASTER(MYTIMES) uses times MYTIMES (in sec), can be a vector or a
  6. % cell
  7. %
  8. % PLOTRASTER(MYTIMES, TIMELIMS) plot from TIMELIMS(1) to TIMELIMS(2) (in
  9. % sec)
  10. rasterColor = [0 0 0]; % plot spikes in black
  11. if isempty(myTimes)
  12. warning('nothing to plot')
  13. return
  14. end
  15. % initalize
  16. timeEdges = timeLims(1):0.001:timeLims(2);
  17. nTimes = length(timeEdges);
  18. myTimes = myTimes(:);
  19. nTrials = length(myTimes);
  20. % create raster matrix
  21. rasterMat = zeros(nTrials, nTimes);
  22. for irow = 1:size(rasterMat,1)
  23. if ~isempty(myTimes{irow})
  24. rasterMat(irow,:) = histc(myTimes{irow}, timeEdges);
  25. end
  26. end
  27. % compute burst indices
  28. % check if rasters are plotted in red
  29. burstColor = repmat([1 0 0], size(rasterColor, 1), 1);
  30. if any(cellfun(@(x) isequal(x, [1 0 0]), num2cell(rasterColor, 2)))
  31. warning('plotRaster:labelBursts', 'Burst spikes will be labelled red, and rasterColor is red as well!');
  32. end
  33. dtsilent = 0.100; % 100 ms silence before spike
  34. dtburst = 0.004; % next spike within 4 ms
  35. iburst = cell(1, nTrials);
  36. itonic = cell(1, nTrials);
  37. for itrial = 1 : nTrials
  38. [iburst{itrial},itonic{itrial}] = FindBurstSpikes(myTimes{itrial},dtburst,dtsilent);
  39. end
  40. % compute burst matrix
  41. burstMat = zeros(nTrials, nTimes);
  42. for irow = 1:size(burstMat,1)
  43. if isempty(iburst{irow})
  44. continue
  45. end
  46. burstMat(irow,:) = histc(myTimes{irow}(iburst{irow}), timeEdges);
  47. end
  48. % plot the rasters
  49. [trials,timebins] = find(rasterMat); % Find the trial (yPoints) and timebin (xPoints) of each spike
  50. trials = trials(:)';
  51. timebins = timebins(:)';
  52. spikeHeight = 0.5;
  53. % the first two dimensions represent the coordinates for the vertical lines
  54. % the third dimension containing NaNs works as a separator between the
  55. % vertical lines
  56. xPoints = [timebins; timebins; NaN(size(timebins))]*0.001 + timeEdges(1);
  57. yPoints = [trials - spikeHeight; trials + spikeHeight; NaN(size(trials))]*(1/nTrials);
  58. plot(xPoints, yPoints, 'Color',rasterColor);
  59. [trials,timebins] = find(burstMat);
  60. trials = trials';
  61. timebins = timebins';
  62. xPoints = [timebins; timebins; NaN(size(timebins))]*0.001 + timeEdges(1);
  63. yPoints = [trials - spikeHeight; trials + spikeHeight; NaN(size(trials))]*(1/nTrials);
  64. plot(xPoints, yPoints, 'Color', burstColor, 'linewidth', 1);
  65. hold off
  66. allAxH = gca;
  67. spaceHolder = spikeHeight*1/nTrials; % adds space for the first row of spikes
  68. set(allAxH, 'xlim', timeLims, 'ylim', [-inf 1+spaceHolder], 'TickDir', 'out', 'Box', 'off');
  69. xlabel(allAxH(end), 'Time (s)');