Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

How to: Control Histogram bin width

Asked by Hello kity on 4 Feb 2013

Hi

Matlab chooses its own bin width when u use hist().

But I want, for example to make an histogram which makes bins of each 0.5 width. I noticed that hist uses the entire data range as range.

bin_width ( get from edit field)

data=[1:10];
range=max(data)-min(data);
bins= bin_width * range
[n xout]= hist (data, bins)

this still makes random hist where u dont have control over the bins.

what i want is,select the bin_width , 0.5 , plot first bin between 0 - 0.5 , second 0.5- 1 etc.

0 Comments

Hello kity

Tags

Products

No products are associated with this question.

2 Answers

Answer by per isakson on 4 Feb 2013
Accepted answer

Try

    doc histc

0 Comments

per isakson
Answer by Hello kity on 4 Feb 2013
Edited by Hello kity on 4 Feb 2013

I found an answer at google, adjusted some, hope it is useful.

With this code you can manually select the stepsize of the hist bins, for example, stepsize is 1. then it plots a bin from 0-1 ,1- 2 etc. IT also plots the amount of each bin on top of it.

xStep=str2double(get(handles.xstep1,'String'));    % edit field where is used a input for xstep        
        hist(DATA)   
                 xbounds = xlim;
          nBins = (xbounds(2)-xbounds(1))/xStep;
          edges = linspace(xbounds(1),xbounds(2), nBins+1);
          %# compute center of bins (used as x-coord for labels)
          bins = ( edges(1:end-1) + edges(2:end) ) / 2;
          %# histc
          [counts,binIdx] = histc(DATA, edges);
          counts(end-1) = sum(counts(end-1:end));  %# combine last two bins
          counts(end) = [];                        %#
   %         %# plot histogram
          bar(edges(1:end-1), counts, 'histc')
                      for c=1:numel(counts)
                          if counts(c)~=0
                              text(bins(c), counts(c)+0.3, num2str(counts(c)), 'FontWeight','bold', 'FontSize',8, ...
                                  'VerticalAlignment','bottom', 'HorizontalAlignment','center');
                          end
                      end
                      set(gca,'ylim',[0 max(counts)+1/10*(max(counts))]); %adjust ymax
          end

0 Comments

Hello kity

Contact us