MATLAB: Finding Closest Data Point

closest data pointspikestime

I have one matrix with data in the first column and time stamps in the second column (datamatrix.mat). The next matrix contains spiketimes (spiketimematrix.mat). I want to get the data point in the first column of first matrix that is the closest time point corresponding to the spike times in spiketimematrix.mat. For example, the first spiketime is 166.1670, which corresponds to the closet time point of 166.1696 and corresponds with the data point 2.5281.

Best Answer

Try something like
% Find all time differences:
timeDiffs = abs(timeStamps - spikeTimes);
% Find out which has the smallest difference:
[minTimeDiff, indexOfMin] = min(timeDiffs);
% Get the value from column 1.
result = dataColumn1(indexOfMin)