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

  1. function errorbar(x, y, std)
  2. %ERRORBAR Plot graph with error bars.
  3. %    ERRORBAR(X,Y,E) plots the graph of vector X vs. vector Y with
  4. %    error bars specified by the vector E.  The vectors X,Y and E must
  5. %    be the same length.  If X,Y, and E are matrices then each column
  6. %    produces a separate line.  The error bars are each drawn a distance
  7. %    of E(i) above and below the points in (X,Y) so that each bar 
  8. %    is 2*E(i) long.
  9. %
  10. %    ERRORBAR(Y,E) plots Y with error bars E.
  11. %
  12. %    For example,
  13. %
  14. %       x = 1:10;
  15. %       y = sin(x);
  16. %       e = std(y)*ones(size(x));
  17. %       errorbar(x,y,e)
  18. %
  19. %    draws error bars of unit standard deviation.
  20.  
  21. %    L. Shure 5-17-88, 10-1-91
  22. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  23.  
  24. if min(size(x))==1,
  25.   npt = length(x);
  26.   x = x(:);
  27.   y = y(:);
  28.   if nargin>2,  std = std(:); end
  29. else
  30.   [npt,n] = size(x);
  31. end
  32.  
  33. if nargin == 2
  34.   std = y;
  35.   y = x;
  36.   [m,n] = size(y);
  37.   x(:) = [1:npt]'*ones(1,n);;
  38. end
  39. if isstr(x) | isstr(y) | isstr(std)
  40.     error('Arguments must be numeric.')
  41. end
  42.  
  43. if any(size(x)~=size(y)) | any(size(x)~=size(std)),
  44.   error('The sizes of X, Y, and E must be the same.');
  45. end
  46.  
  47. tee = (max(x(:))-min(x(:)))/100;  % make tee .02 x-distance for error bars
  48. xl = x - tee;
  49. xr = x + tee;
  50. n = size(y,2);
  51.  
  52. % Plot graph and bars
  53. cax = newplot;
  54. next = lower(get(cax,'NextPlot'));
  55. % build up nan-separated vector for bars
  56. xb = [];
  57. yb = [];
  58. nnan = nan*ones(1,n);
  59. for i = 1:npt
  60.     ytop = y(i,:) + std(i,:);
  61.     ybot = y(i,:) - std(i,:);
  62.     xb = [xb; x(i,:); x(i,:) ; nnan; xl(i,:);xr(i,:);nnan ;xl(i,:);xr(i,:);nnan];
  63.     yb = [yb; ytop;ybot;nnan;ytop;ytop;nnan;ybot;ybot;nnan];
  64. end
  65.  
  66. plot(xb,yb,x,y,'-')
  67.