MATLAB Answers


Binning data into 1 ms bins and count events

Asked by PAK
on 24 Aug 2018
Latest activity Commented on by PAK
on 30 Aug 2018
Hi Everyone,
I have an experiment that is recorded over a timeperiod = 2.63610e+03 (in seconds). During my experiment, I was recording events. My output of that is a cell matrix, where each cell in the first row is equivalent to a list (in the form of a double matrix) of times where the event occurred.
How would I bin my time into 1 ms bins and then count how many events occur in each bin? I would like to calculate an average spike rate over the whole trial.
As always, thank you!


Sign in to comment.




1 Answer

Answer by Image Analyst
on 24 Aug 2018
 Accepted Answer

Try histogram() or histcounts().
First scan each cell in the first row of the cell array to get the max and min times so we can determine the edges for your histogram. Then scan again getting counts of events in each cell and add them in to the overall/master histogram. Something like (untested):
minTime = inf;
maxTime = -inf;
[rows, columns] = size(ca); % Get size of cell array
for col = 1 : columns
theseTimes = ca{1, col};
thisMin = min(theseTimes);
thisMax = max(theseTimes);
minTime = min(minTime, thisMin);
maxTime = max(maxTime, thisMax);
% Now construct edges
edges = minTime : 0.001 : (maxTime + 0.001);
% Now get histogram of all cells in first row.
allCounts = zeros(1, length(edges) - 1);
for col = 1 : columns
theseTimes = ca{1, col};
[counts, values] = histcounts(theseTimes, edges);
allCounts = allCounts + counts;
% Plot it
bar(edges(1:end-1), allCounts);
grid on;
Adapt/rename/fix as needed. Attach your data if you need more help.

  1 Comment

This worked really well! Thank you!!

Sign in to comment.