HIST function with negative center and single event.

4 views (last 30 days)
Dear all,
My program runs into issue with 'hist' when certain criteria meet. Following is the code:
tr_min_center = floor(min(spike_times)/bin_size)*bin_size + (.5*bin_size);
tr_max_center = ceil(max(spike_times)/bin_size)*bin_size - (.5*bin_size);
tr_centers = tr_min_center:bin_size:tr_max_center;
[tr_n, tr_xout] = hist(spike_times, tr_centers);
When there is only one event in spike_time with a negative number (relate to a synchronization time point), so there is only one negative center, function 'hist' give out error like: Attempted to access xx(0); index must be a positive integer or logical.
I track it down in 'hist' at code line 67 to 75:
if length(x) == 1
if miny == maxy,
miny = miny - floor(x/2) - 0.5;
maxy = maxy + ceil(x/2) - 0.5;
end
binwidth = (maxy - miny) ./ x;
xx = miny + binwidth*(0:x); -- This is the line error occurred since x is a negative number.
Could someone help provide a fix. Thank you!

Accepted Answer

Jan
Jan on 5 May 2011
As the documentation of HIST explains, "HIST(Y, M)" uses M bins, if M is a scalar. Using a negative or not integer number of bins must fail.
Check the number of bins before calling HIST: For a single bin the calculations are very cheap.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!