From octave-sources-request at bevo dot che dot wisc dot edu Wed May 9 05:23:43 2001 Subject: bar.m improvements From: Walery Studennikov To: octave-sources at bevo dot che dot wisc dot edu Date: Wed, 9 May 2001 15:27:40 -0400 --vkogqOf2sHV7VnPd Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Here are my version of bar.m with some improvements: Now it is possible to specify bars width by the third argument --vkogqOf2sHV7VnPd Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="bar.m" ## Copyright (C) 1996, 1997 John W. Eaton ## ## This file is part of Octave. ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2, or (at your option) ## any later version. ## ## Octave is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, write to the Free ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA ## 02111-1307, USA. ## -*- texinfo -*- ## at deftypefn {Function File} {} bar (@var{x}, @var{y}) ## Given two vectors of x-y data, at code{bar} produces a bar graph. ## ## If only one argument is given, it is taken as a vector of y-values ## and the x coordinates are taken to be the indices of the elements. ## ## If two output arguments are specified, the data are generated but ## not plotted. For example, ## ## at example ## bar (x, y); ## at end example ## ## at noindent ## and ## ## at example ## [xb, yb] = bar (x, y); ## plot (xb, yb); ## at end example ## ## at noindent ## are equivalent. ## at end deftypefn ## at seealso{plot, semilogx, semilogy, loglog, polar, mesh, contour, ## stairs, gplot, gsplot, replot, xlabel, ylabel, and title} ## Author: jwe function [xb, yb] = bar (x, y, wid) width = 0.8; if (nargin == 1 || is_scalar(y) ) if nargin == 2 width = y; endif space = (1 - width)/2; if (is_vector (x)) len = 4 * length (x) + 2; tmp_xb = tmp_yb = zeros (len, 1); tmp_xb(1) = 0.5; tmp_yb(1) = 0.0; k = 0.5; for i = 2:4:len-1 tmp_xb(i) = k + space; tmp_xb(i+1) = k + space; tmp_xb(i+2) = k + space + width; tmp_xb(i+3) = k + space + width; tmp_yb(i) = 0.0; tmp_yb(i+1) = x(k); tmp_yb(i+2) = x(k); tmp_yb(i+3) = 0.0; k++; endfor tmp_xb(len) = k; tmp_yb(len) = 0.0; else error ("bar: argument must be a vector"); endif elseif (nargin == 2 || is_scalar(wid)) if (is_vector (x) && is_vector (y)) if nargin == 3 width = wid; endif xlen = length (x); ylen = length (y); if (xlen == ylen) len = 4 * xlen; tmp_xb = tmp_yb = zeros (len, 1); cutoff = zeros (1, xlen-1); for i = 1:xlen-1 cutoff(i) = (x(i) + x(i+1)) / 2.0; endfor delta_p = cutoff(1) - x(1); delta_m = delta_p; k = 1; for i = 1:4:len tmp_xb(i+0) = x(k) - delta_m * width; tmp_xb(i+1) = tmp_xb(i+0); tmp_xb(i+2) = x(k) + delta_p * width; tmp_xb(i+3) = tmp_xb(i+2); tmp_yb(i+0) = 0.0; tmp_yb(i+1) = y(k); tmp_yb(i+2) = y(k); tmp_yb(i+3) = 0.0; if (k < xlen) if (x(k+1) < x(k)) error ("bar: x vector values must be in ascending order"); endif delta_m = x(k+1) - cutoff(k); k++; if (k < xlen) delta_p = cutoff(k) - x(k); else delta_p = delta_m; endif endif endfor else error ("bar: arguments must be the same length"); endif else error ("bar: arguments must be vectors"); endif else usage ("[xb, yb] = bar (x, y)"); endif if (nargout == 0) plot (tmp_xb, tmp_yb); else xb = tmp_xb; yb = tmp_yb; endif endfunction --vkogqOf2sHV7VnPd-- ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------