home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / PLOTXY.DI$ / BAR.M next >
Encoding:
Text File  |  1993-03-07  |  3.0 KB  |  107 lines

  1. function [xo,yo] = bar(x,y,c)
  2. %BAR    Bar graph.
  3. %    BAR(Y) draws a bar graph of the elements of vector Y.
  4. %    BAR(X,Y) draws a bar graph of the elements of vector Y at
  5. %    the locations specified in vector X.  The X-values must
  6. %    be in ascending order.  If the X-values are not evenly spaced, the
  7. %    interval chosen is not symmetric about each data point.  Instead,
  8. %    the bars are drawn midway between adjacent X-values.  The endpoints
  9. %    simply adopt the internal intervals for the external ones needed.
  10. %
  11. %    If X and Y are matrices the same size, one bar graph per column 
  12. %    is drawn.
  13. %
  14. %    [XX,YY] = BAR(X,Y) does not draw a graph, but returns vectors
  15. %    X and Y such that PLOT(XX,YY) is the bar chart.
  16. %
  17. %    See also STAIRS, HIST.
  18. %
  19. %    BAR(X,'linetype') or BAR(X,Y,'linetype') uses the plot linetype
  20. %    specified.  See PLOT for details.
  21.  
  22. %    C.B Moler 2-06-86
  23. %    Modified 24-Dec-88, LS.
  24. %    Modified 8-5-91 by cmt, 1-2-92, ls.
  25. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  26.                
  27. if nargin == 1
  28.   if isstr(x)
  29.     error('First argument must be numeric.');
  30.   end
  31.   if min(size(x))==1, y = x(:); else y = x; end
  32.   [n,m] = size(y);
  33.   x = [1:n]'*ones(1,m);
  34.   c = '-';
  35. elseif nargin >= 2
  36.   if isstr(x)
  37.     error('First argument must be numeric.');
  38.   end
  39.   if isstr(y),
  40.     c = y;
  41.     if min(size(x))==1, y = x(:); else y = x; end
  42.     [n,m] = size(y);
  43.     x = [1:n]'*ones(1,m);
  44.   else
  45.     if min(size(y))==1, y = y(:); end
  46.     [n,m] = size(y);
  47.     if min(size(x))==1, x = x(:)*ones(1,m); end
  48.     if nargin~=3, c = '-'; end
  49.   end
  50. end
  51.  
  52. nn = 5*n;
  53. yy = zeros(nn+1,m);
  54. xx = yy;
  55. yy(3:5:nn,:) = y;
  56. yy(4:5:nn,:) = y;
  57.  
  58. notequal = max(abs(diff(diff(x)))) > max(max(abs(x)))*sqrt(eps);
  59.  
  60. if max(diff(x))==0, notequal=[]; end % Special case 
  61.  
  62. if isempty(notequal), % Scalar case and special case
  63.     delta = 1;
  64.     t = x - 0.5*delta;
  65.     xx(1:5:nn,:) = t;
  66.     xx(2:5:nn,:) = t + 0.1*delta;
  67.     xx(3:5:nn,:) = t + 0.1*delta;
  68.     xx(4:5:nn,:) = t + 0.9*delta;
  69.     xx(5:5:nn,:) = t + 0.9*delta;
  70.     xx(nn+1,:) = xx(nn,:)+0.1*delta;
  71. elseif ~notequal
  72.     delta = ones(n,1) * (max(x) - min(x)) / (n-1);
  73.     t = x - 0.5*delta;
  74.     xx(1:5:nn,:) = t;
  75.     xx(2:5:nn,:) = t + 0.1*delta;
  76.     xx(3:5:nn,:) = t + 0.1*delta;
  77.     xx(4:5:nn,:) = t + 0.9*delta;
  78.     xx(5:5:nn,:) = t + 0.9*delta;
  79.     xx(nn+1,:) = xx(nn,:);
  80. else    % spacing is unequal - do the best you can
  81.     dx = diff(x)/2;
  82.     xx(1:5:nn+1,:) = [x;x(n,:)] - [dx(1,:);dx;dx(n-1,:)];
  83.     xx(2:5:nn+1,:) = x - 0.9*[dx(1,:);dx];
  84.     xx(3:5:nn+1,:) = xx(2:5:nn+1,:);
  85.     xx(4:5:nn+1,:) = x + 0.9*[dx;dx(n-1,:)];
  86.     xx(5:5:nn+1,:) = xx(4:5:nn+1,:);
  87.     xx(nn,:) = x(n,:) + 0.9*dx(n-1,:);
  88.     xx(nn+1,:) = xx(nn,:);
  89. end
  90.  
  91. if nargout == 0
  92.     cax = newplot;
  93.     next = lower(get(cax,'NextPlot'));
  94.     hold_state = ishold;
  95.     plot(xx,yy,c)
  96.     axy = axis;
  97.     hold on;
  98.     plot([axy(1) axy(2)],[0 0],'w-')
  99.     if notequal
  100.         plot(x,zeros(size(x)),'*')
  101.     end
  102.     if ~hold_state, set(cax,'NextPlot',next); end
  103. else
  104.     xo = xx;
  105.     yo = yy;
  106. end
  107.