home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / plot / stairs.m < prev    next >
Text File  |  1999-04-29  |  3KB  |  103 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: [xs, ys] = stairs (x, y)
  21. ##
  22. ## Given two vectors of x-y data, stairs produces a `stairstep' plot.
  23. ##
  24. ## If only one argument is given, it is taken as a vector of y-values
  25. ## and the x coordiates 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. ##   stairs (x, y);
  31. ##
  32. ## and
  33. ##
  34. ##   [xs, ys] = stairs (x, y);
  35. ##   plot (xs, ys);
  36. ##
  37. ## are equivalent.
  38. ##
  39. ## See also: plot, semilogx, semilogy, loglog, polar, mesh, contour,
  40. ##           bar, gplot, gsplot, replot, xlabel, ylabel, title
  41.  
  42. ## Author: jwe
  43.  
  44. function [xs, ys] = stairs (x, y)
  45.  
  46.  
  47.   if (nargin == 1)
  48.     if (is_vec (x))
  49.       len = 2 * length (x);
  50.       tmp_xs = tmp_ys = zeros (len, 1);
  51.       k = 0;
  52.       for i = 1:2:len
  53.         tmp_xs(i) = k++;
  54.         tmp_ys(i) = x(k);
  55.         tmp_ys(i+1) = x(k);
  56.         tmp_xs(i+1) = k;
  57.       endfor
  58.     else
  59.       error ("stairs: argument must be a vector");
  60.     endif
  61.   elseif (nargin == 2)
  62.     if (is_vec (x) && is_vec (y))
  63.       xlen = length (x);
  64.       ylen = length (y);
  65.       if (xlen == ylen)
  66.         len = 2 * xlen;
  67.         tmp_xs = tmp_ys = zeros (len, 1);
  68.     k = 1;
  69.         len_m2 = len - 2;
  70.     for i = 1:2:len_m2
  71.       tmp_xs(i) = x(k);
  72.       tmp_ys(i) = y(k);
  73.       tmp_ys(i+1) = y(k);
  74.           k++;
  75.       tmp_xs(i+1) = x(k);
  76.           if (x(k) < x(k-1))
  77.             error ("stairs: x vector values must be in ascending order");
  78.           endif
  79.     endfor
  80.         tmp_xs(len-1) = x(xlen);
  81.         delta = x(xlen) - x(xlen-1);
  82.         tmp_xs(len) = x(xlen) + delta;
  83.         tmp_ys(len-1) = y(ylen);
  84.         tmp_ys(len) = y(ylen);
  85.       else
  86.         error ("stairs: arguments must be the same length");
  87.       endif
  88.     else
  89.       error ("stairs: arguments must be vectors");
  90.     endif
  91.   else
  92.     usage ("[xs, ys] = stairs (x, y)");
  93.   endif
  94.  
  95.   if (nargout == 0)
  96.     plot (tmp_xs, tmp_ys);
  97.   else
  98.     xs = tmp_xs;
  99.     ys = tmp_ys;
  100.   endif
  101.  
  102. endfunction
  103.