function dataplot(data,gauss)
%Plots a single-dimensional, labeled data
%
%Syntax:
% dataplot(data)
% dataplot(data,gauss)
%
%Parameters:
% The first parameter 'data' is a matrix of 2 columns. The first
% columns contains numeric values. The second column contains
% value labels. All values with the same label are ploted into
% a single histogram.
%
% The histogram plot can be augmented with a plot of gaussians.
% Their means and variances are supplied in the 'gauss' matrix,
% whose first column contains the mean (mu) and second column
% contains the variance (sigma^2). Each row will be plotted
% as a single gaussian.
%
%Example:
% dataplot([100 1; 100.1 1; 100 2; 120 1], [105 4; 110 10]);
%   Plots two histograms: The first one from values
%   {100, 100.1, 120} and the second one from {100}.
%   Moreover plots two gaussians: the first with mu=105,
%   sigma=4 and the second with mu=110, sigma=10.
%
%See also:
%  hist, plot

v = data(:,1);
c = data(:,2);

col = [0.5 1 0.5; 1 1 0; 0 0 1; 1 0 0];
cls = unique(c);
cln = size(cls,1);
step = (max(v) - min(v)) / 5;
hor  = min(v) : step : max(v);
skip = 0.3;

for i = 1:cln
    if i == 1
        hold off
    else
        hold on
    end

    [bin_vals,x_axis] = hist( v(c == cls(i)), hor);
    bin_vals = 100 * bin_vals / sum(bin_vals);
    
    bar_shift = (i-1) * step / (cln+skip);
    bar(x_axis + bar_shift, bin_vals, 1/(cln+skip));
end

% Add labels and a grid
xlabel 'Height of a person [cm]'
ylabel 'Frequency in population [%]'
grid on

% Set 50% opacity for all bar graphs
plot_handle = findobj(gca,'Type','patch');
for i = 1:cln
  set(plot_handle(i),'FaceAlpha', 0.5);
  set(plot_handle(i),'EdgeAlpha', 0.0);
  set(plot_handle(i),'FaceColor', col(1+mod(i-1,size(col,1)), :));
end

% Is the second parameter supplied?
if exist('gauss', 'var')
  x_axis = linspace(min(v), max(v), 1000);
  
  % Print each gaussian distribution
  for i = 1:size(gauss, 1)
    y_vals = normpdf(x_axis, gauss(i,1), gauss(i,2));
    y_vals = 100 * y_vals; % in percentages
    
    color = col(1+mod(i+cln-1,size(col,1)), :);
    %color = color * 0.6; % darken the colors
    plot(x_axis, y_vals, 'Color', color, 'LineWidth', 3);
  end
end
