home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / control / com2str.m < prev    next >
Text File  |  1999-03-05  |  2KB  |  76 lines

  1. # Copyright (C) 1998 A. Scottedward Hodel 
  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 the 
  7. # Free Software Foundation; either version 2, or (at your option) any 
  8. # later version. 
  9. # Octave is distributed in the hope that it will be useful, but WITHOUT 
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  11. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
  12. # for more details.
  13. # You should have received a copy of the GNU General Public License 
  14. # along with Octave; see the file COPYING.  If not, write to the Free 
  15. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  16.  
  17. function retval = com2str(zz,flg)
  18. # usage retval = com2str(zz{,flg})
  19. #  
  20. # convert complex number to a string
  21. # zz: complex number
  22. # flg: format flag
  23. #      0 (default):            -1, 0, 1,   1i,   1 + 0.5i
  24. #      1 (for use with zpout): -1, 0, + 1, + 1i, + 1 + 0.5i
  25. #
  26.  
  27.   if (nargin < 1 | nargin > 2)
  28.     usage("com2str(zz{,flg})");
  29.   endif
  30.   if(nargin == 1)
  31.     flg = 0;
  32.   endif
  33.  
  34.   if( !(is_scalar(zz) & is_scalar(flg) ) )
  35.     error("com2str: arguments must be a scalar.");
  36.   endif
  37.  
  38.   if(flg != 0 & flg != 1)
  39.     error(["Illegal flg value: ",num2str(flg)]);
  40.   endif
  41.  
  42.   sgns = "+-";
  43.   rz = real(zz);
  44.   iz = imag(zz);
  45.   az = abs(zz);
  46.   if(iz == 0)
  47.     # strictly a real number
  48.     switch(flg)
  49.     case(0)
  50.       retval = num2str(rz);
  51.     case(1)
  52.       retval = [ sgns(1+(rz< 0))," ", num2str(abs(rz))];
  53.     endswitch
  54.   elseif(rz == 0)
  55.     # strictly an imaginary number
  56.     switch(flg)
  57.     case(0)
  58.       retval = num2str(iz);
  59.     case(1)
  60.       retval = [ sgns(1+(iz< 0))," ", num2str(abs(iz)),"i"];
  61.     endswitch
  62.   else
  63.     # complex number
  64.     # strictly an imaginary number
  65.     switch(flg)
  66.     case(0)
  67.       retval = [num2str(rz)," ",com2str(i*iz,1)];
  68.     case(1)
  69.       retval = [ sgns(1+(rz< 0))," ", num2str(abs(rz))," ",com2str(i*iz,1)];
  70.     endswitch
  71.   endif
  72.   
  73. endfunction
  74.