# HG changeset patch # User jwe # Date 1191867824 0 # Node ID 0d11a12643f1e120fb5fa6e15c68183c4045fad0 # Parent 9e32bb1099804b5ca8bf99d3605d205fa2d894b4 [project @ 2007-10-08 18:23:44 by jwe] diff -r 9e32bb109980 -r 0d11a12643f1 scripts/ChangeLog --- a/scripts/ChangeLog Mon Oct 08 11:06:48 2007 +0000 +++ b/scripts/ChangeLog Mon Oct 08 18:23:44 2007 +0000 @@ -1,3 +1,9 @@ +2007-10-08 John Swensen + + * general/num2str.m: Eliminate extra whitespace in output. + * strings/strtrim.m: New function. + * strings/Makefile.in (SOURCES): Add it to the list. + 2007-10-06 John W. Eaton * polynomial/residue.m: New test from test/test_poly.m. diff -r 9e32bb109980 -r 0d11a12643f1 scripts/general/num2str.m --- a/scripts/general/num2str.m Mon Oct 08 11:06:48 2007 +0000 +++ b/scripts/general/num2str.m Mon Oct 08 18:23:44 2007 +0000 @@ -56,11 +56,16 @@ dgt1 = ceil (log10 (max (max (abs (real (x(:)))), max (abs (imag (x(:))))))) + 1; dgt2 = dgt1 - (min (real (x(:))) >= 0); - fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1); + + if (length (abs (x) == x) > 0) + fmt = sprintf("%%%dg%%+-%dgi ", dgt2, dgt1); + else + fmt = sprintf("%%%dd%%+-%ddi ", dgt2, dgt1); + endif elseif (isscalar (x)) - fmt = "%.4g%-+.4gi"; + fmt = "%.6g%-+.6gi"; else - fmt = "%11.4g%-+11.4gi"; + fmt = "%11.6g%-+11.6gi"; endif endif @@ -101,7 +106,7 @@ endwhile tmp(length (tmp)) = ""; - retval = split (tmp, "\n"); + retval = strtrim (split (tmp, "\n")); else if (nargin == 2) if (ischar (arg)) @@ -120,18 +125,22 @@ else dgt = floor (log10 (max (abs(x(:))))) + (min (real (x(:))) < 0) + 1; endif - fmt = sprintf ("%%%dd ", dgt); + if (length (abs (x) == x) > 0) + fmt = sprintf ("%%%dg ", dgt); + else + fmt = sprintf ("%%%dd ", dgt); + endif elseif (isscalar (x)) - fmt = "%.4g"; + fmt = "%11.5g"; else - fmt = "%11.4g"; + fmt = "%11.5g"; endif endif fmt = strcat (deblank (repmat (fmt, 1, columns (x))), "\n"); nd = ndims (x); tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); tmp(length (tmp)) = ""; - retval = split (tmp, "\n"); + retval = strtrim (split (tmp, "\n")); endif endfunction diff -r 9e32bb109980 -r 0d11a12643f1 scripts/strings/Makefile.in --- a/scripts/strings/Makefile.in Mon Oct 08 11:06:48 2007 +0000 +++ b/scripts/strings/Makefile.in Mon Oct 08 18:23:44 2007 +0000 @@ -24,7 +24,8 @@ dec2bin.m dec2hex.m findstr.m hex2dec.m index.m isletter.m \ lower.m mat2str.m rindex.m split.m str2double.m str2mat.m \ str2num.m strcat.m strcmpi.m strfind.m strjust.m strmatch.m \ - strncmpi.m strrep.m strtok.m strtrunc.m strvcat.m substr.m upper.m + strncmpi.m strrep.m strtok.m strtrim.m strtrunc.m strvcat.m \ + substr.m upper.m DISTFILES = $(addprefix $(srcdir)/, Makefile.in $(SOURCES)) diff -r 9e32bb109980 -r 0d11a12643f1 scripts/strings/strtrim.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/strings/strtrim.m Mon Oct 08 18:23:44 2007 +0000 @@ -0,0 +1,53 @@ +## Copyright (C) 1996 Kurt Hornik +## +## 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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- @deftypefn {Function File} {} deblank (@var{s}) +## Remove leading and trailing blanks and nulls from @var{s}. If +## @var{s} is a matrix, @var{deblank} trims each row to the length of +## longest string. If @var{s} is a cell array, operate recursively on +## each element of the cell array. @end deftypefn + +## Author: John Swensen + +## This function was derived from deblank. + +function s = strtrim (s) + + if (nargin != 1) + print_usage (); + endif + + if (ischar (s)) + + k = find (! isspace (s) & s != "\0"); + if (isempty (s) || isempty (k)) + s = ""; + else + s = s(:,ceil (min (k) / rows (s)):ceil (max (k) / rows (s))); + endif + + elseif (iscell(s)) + + s = cellfun (@strtrim, s, "UniformOutput", false); + + else + error ("strtrim: expecting string argument"); + endif + +endfunction