MATLAB: Histogram

histogram plots

Hi, I am trying to make a histogram plot using 2 variables and the resulting frequency should be plotted as a bar graph. I tried using the bar function with the following sample code:
x=rand(10,1); y=rand(10,1); bar(x,y,'hist');
I wanted to bin the values in my x-axis. I don't have any idea on how to do it. Your suggestions will be most appreciated.
Thanks, Irene

Best Answer

What you've put doesn't really make sense. See if this is what you want. It computes the histograms of x and y and then plots them.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 15;
x=rand(10,1);
y=rand(10,1);
% Plot the original x data.
subplot(2, 2, 1);
bar(x);
xlabel('Index of X', 'fontSize', fontSize);
ylabel('Value of X', 'fontSize', fontSize);
title('X as a function of Index', 'fontSize', fontSize);
% Plot the original y data.
subplot(2, 2, 2);
bar(y);
xlabel('Index of Y', 'fontSize', fontSize);
ylabel('Value of Y', 'fontSize', fontSize);
title('Y as a function of Index', 'fontSize', fontSize);
% Get the histograms.
[xCounts xValues] = hist(x)
[yCounts yValues] = hist(y)
% Plot X histogram.
subplot(2, 2, 3);
bar(xValues, xCounts);
xlabel('Value of X', 'fontSize', fontSize);
ylabel('Frequency of Occurrence for X', 'fontSize', fontSize);
grid on;
title('Histogram of X', 'fontSize', fontSize);
subplot(2, 2, 4);
bar(yValues, yCounts);
xlabel('Value of Y', 'fontSize', fontSize);
ylabel('Frequency of Occurrence forY', 'fontSize', fontSize);
grid on;
title('Histogram of Y', 'fontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));