home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / plot / bar.m < prev    next >
Text File  |  1999-04-29  |  3KB  |  109 lines

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## usage: [xb, yb] = bar (x, y)
  21. ##
  22. ## Given two vectors of x-y data, bar produces a `bar' graph.
  23. ##
  24. ## If only one argument is given, it is taken as a vector of y-values
  25. ## and the x coordinates are taken to be the indices of the elements.
  26. ##
  27. ## If two output arguments are specified, the data are generated but
  28. ## not plotted.  For example,
  29. ##
  30. ##   bar (x, y);
  31. ##
  32. ## and
  33. ##
  34. ##   [xb, yb] = bar (x, y);
  35. ##   plot (xb, yb);
  36. ##
  37. ## are equivalent.
  38. ##
  39. ## See also: plot, semilogx, semilogy, loglog, polar, mesh, contour,
  40. ##           stairs, gplot, gsplot, replot, xlabel, ylabel, title
  41.  
  42. ## Author: jwe
  43.  
  44. function [xb, yb] = bar (x, y)
  45.  
  46.   if (nargin == 1)
  47.     if (is_vec (x))
  48.       len = 3 * length (x) + 1;
  49.       tmp_xb = tmp_yb = zeros (len, 1);
  50.       tmp_xb(1) = 0.5;
  51.       tmp_yb(1) = 0;
  52.       k = 1;
  53.       for i = 2:3:len
  54.         tmp_xb(i) = k-0.5;
  55.         tmp_xb(i+1) = k+0.5;
  56.         tmp_xb(i+2) = k+0.5;
  57.         tmp_yb(i) = x(k);
  58.         tmp_yb(i+1) = x(k);
  59.         tmp_yb(i+2) = 0.0;
  60.         k++;
  61.       endfor
  62.     else
  63.       error ("bar: argument must be a vector");
  64.     endif
  65.   elseif (nargin == 2)
  66.     if (is_vec (x) && is_vec (y))
  67.       xlen = length (x);
  68.       ylen = length (y);
  69.       if (xlen == ylen)
  70.         len = 3 * xlen + 1;
  71.         tmp_xb = tmp_yb = zeros (len, 1);
  72.         delta = (x(2) - x(1)) / 2.0;
  73.         tmp_xb(1) = x(1) - delta;
  74.         tmp_yb(1) = 0.0;
  75.     k = 1;
  76.         for i = 2:3:len
  77.           tmp_xb(i) = tmp_xb(i-1);
  78.           tmp_xb(i+1) = tmp_xb(i) + 2.0 * delta;
  79.           tmp_xb(i+2) = tmp_xb(i+1);
  80.       tmp_yb(i) = y(k);
  81.       tmp_yb(i+1) = y(k);
  82.       tmp_yb(i+2) = 0.0;
  83.           if (k < xlen)
  84.             delta = (x(k+1) - x(k)) / 2.0;
  85.             if (x(k+1) < x(k))
  86.               error ("bar: x vector values must be in ascending order");
  87.             endif
  88.           endif
  89.           k++;
  90.     endfor
  91.       else
  92.         error ("bar: arguments must be the same length");
  93.       endif
  94.     else
  95.       error ("bar: arguments must be vectors");
  96.     endif
  97.   else
  98.     usage ("[xb, yb] = bar (x, y)");
  99.   endif
  100.  
  101.   if (nargout == 0)
  102.     plot (tmp_xb, tmp_yb);
  103.   else
  104.     xb = tmp_xb;
  105.     yb = tmp_yb;
  106.   endif
  107.  
  108. endfunction
  109.