Home > atmlab > graphs > histout2plotvecs.m

histout2plotvecs

PURPOSE ^

HISTOUT2PLOTVECS Convert the output of matlab's hist command to two

SYNOPSIS ^

function [x,y] = histout2plotvecs(n,xout)

DESCRIPTION ^

 HISTOUT2PLOTVECS Convert the output of matlab's hist command to two
 vectors that can be plotted with plot.
 
 This function is handy to make histogram plots where only the top
 contour is shown, not the bars.  In that style, several histograms
 can be plotted in one graph.

 The trick is that we are not just connecting the histogram points
 by a straight line, but we plot little stairs corresponding to
 the histogram bins.

 FORMAT A typical calling sequence is:
        [n,xout] = hist(...);
        [x,y]    = histout2plotvecs(n,xout);
        plot(x,y);

 OUT    x,y     Two vectors that can be plotted by plot(x,y)
 IN     n       Frequencies of occurence (hist command output)
        xout    Bin locations (hist command output)

 2008-5-27 Created by Stefan Buehler

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

histout2plotvecs.m

SOURCE CODE ^

0001 % HISTOUT2PLOTVECS Convert the output of matlab's hist command to two
0002 % vectors that can be plotted with plot.
0003 %
0004 % This function is handy to make histogram plots where only the top
0005 % contour is shown, not the bars.  In that style, several histograms
0006 % can be plotted in one graph.
0007 %
0008 % The trick is that we are not just connecting the histogram points
0009 % by a straight line, but we plot little stairs corresponding to
0010 % the histogram bins.
0011 %
0012 % FORMAT A typical calling sequence is:
0013 %        [n,xout] = hist(...);
0014 %        [x,y]    = histout2plotvecs(n,xout);
0015 %        plot(x,y);
0016 %
0017 % OUT    x,y     Two vectors that can be plotted by plot(x,y)
0018 % IN     n       Frequencies of occurence (hist command output)
0019 %        xout    Bin locations (hist command output)
0020 %
0021 % 2008-5-27 Created by Stefan Buehler
0022 
0023 function [x,y] = histout2plotvecs(n,xout)
0024 
0025 % We assume that xout increases monotonically.
0026 
0027 % Always make sure the data are row vectors (e.g. incase input doesn't come from hist)
0028 n=n(:)';
0029 xout=xout(:)';
0030 
0031 % Halv the distance between adjacent points in x:
0032 d = diff(xout)/2;
0033 
0034 % We put the boundaries in the middle between the points in
0035 % xout. Furthermore, we add extra points at the bottom and at the
0036 % top, so that in total boundaries has one element more than xout.
0037 boundaries = [xout(1)-d(1), xout(1:end-1)+d, xout(end)+d(end)];
0038 
0039 % For x, we have to double all points in boundaries. (This will give
0040 % the plot the staircase look.)
0041 %
0042 % The trick we use to calculate this efficiently is that a matrix can
0043 % be interpreted as one continuous vector:
0044 x = [boundaries;...
0045      boundaries];
0046 x = x(1:end);
0047 
0048 % We use the same trick to get the y, but then we must also add
0049 % zeros at both ends:
0050 y = [n;...
0051      n];
0052 y = [0, y(1:end), 0];
0053 
0054 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005