function cogs109hist(data, nbins);
%COGS109HIST Plots a histogram in a new figure window
%
%   COGS109HIST(data, nbins) plots a histogram in a new figure window bypassing the bar
%   function which is a problem on some of the lab computers because they have
%   another bar function which is causing issues with the built-in HIST
%   function.
%
%   USAGE: Takes inputs of DATA (the data you want to make a histogram
%   from, and input of NBINS, which is the number of bins you want the data
%   split into (ie this is the number of bars you will see).
%
%   To use this drop it into your matlab path somewhere.  Or put it in the
%   same directory you are currently using for your homework.
%
%   EXAMPLE:  
%      %create a random vector of data...
%      d = rand(1000,1);
%
%      %And plot a histogram with 20 bins using our function...
%      cogs109hist(d, 20);
%
%   See also HIST, PLOT, AXIS, RAND, PATH, BAR, LEGEND, XLABEL, YLABEL,
%   TITLE.
%
%   Authors: Alex Simpkins and Nick Butko, University of California, San
%   Diego Fall 2006
%
%   Written for CogSci109, Modeling and Data Analysis
%

[num_elements, bin_centers] = hist(data, nbins);
figure;
w = abs(bin_centers(2)-bin_centers(1));

if (w<=0)
   w= max(bin_centers)/nbins;
end

for i=1:nbins,
    x = bin_centers(i)-w/2;
    h = abs(num_elements(i));
    if (h<=0)
        h=max(num_elements)/1000;
    end
    rectangle('Position', [x 0 w h],'FaceColor','r')
end

%axis([0 (nbins*w+w) 0 (max(num_elements)+max(num_elements)/30)]);
