2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
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. |
245
|
19 |
3361
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} int2str (@var{n}) |
4229
|
22 ## @deftypefnx {Function File} {} num2str (@var{x}, @var{precision}) |
|
23 ## @deftypefnx {Function File} {} num2str (@var{x}, @var{format}) |
3361
|
24 ## Convert a number to a string. These functions are not very flexible, |
|
25 ## but are provided for compatibility with @sc{Matlab}. For better control |
|
26 ## over the results, use @code{sprintf} (@pxref{Formatted Output}). |
|
27 ## @end deftypefn |
3408
|
28 ## @seealso{sprintf and int2str} |
4
|
29 |
2314
|
30 ## Author: jwe |
|
31 |
4229
|
32 function retval = num2str (x, arg) |
4
|
33 |
4229
|
34 if (nargin == 1 || nargin == 2) |
|
35 if (nargin == 2) |
|
36 if (isstr (arg)) |
|
37 fmt = arg; |
|
38 else |
|
39 fmt = sprintf ("%%.%dg", arg); |
|
40 endif |
4
|
41 else |
4295
|
42 if (isscalar (x)) |
|
43 fmt = "%.4g"; |
|
44 else |
|
45 fmt = "%11.4g"; |
|
46 endif |
4229
|
47 endif |
|
48 if (iscomplex (x)) |
|
49 error ("num2str: sorry, can't handle complex numbers yet"); |
|
50 else |
|
51 fmt = strcat (repmat (fmt, 1, columns (x)), "\n"); |
|
52 tmp = sprintf (fmt, x.'); |
|
53 tmp(length (tmp)) = ""; |
|
54 retval = split (tmp, "\n"); |
4
|
55 endif |
|
56 else |
4229
|
57 usage ("num2str (x) or num2str (x, precision) or num2str (x, fmt)"); |
4
|
58 endif |
|
59 |
|
60 endfunction |