5837
|
1 ## Copyright (C) 2002 Rolf Fabian <fabian@tu-cottbus.de> |
|
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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {@var{s} =} mat2str (@var{x}, @var{n}) |
|
22 ## @deftypefnx {Function File} {@var{s} =} mat2str (@dots{}, 'class') |
|
23 ## |
|
24 ## Format real/complex numerial matrices as strings. This function |
|
25 ## returns values that are suitable for the use of the @code{eval} |
|
26 ## function. |
|
27 ## |
|
28 ## The precision of the values is given by @var{n}. If @var{n} is a |
|
29 ## scalar then both real and imaginary parts of the matrix are printed |
|
30 ## to the same precision. Otherwise @code{@var{n} (1)} defines the |
|
31 ## precision of the real part and @code{@var{n} (2)} defines the |
|
32 ## precision of the imaginary part. The default for @var{n} is 17. |
|
33 ## |
|
34 ## If the argument 'class' is given, then the class of @var{x} is |
|
35 ## included in the string in such a way that the eval will result in the |
|
36 ## construction of a matrix of the same class. |
|
37 ## |
|
38 ## @example |
|
39 ## @group |
|
40 ## mat2str( [ -1/3 + i/7; 1/3 - i/7 ], [4 2] ) |
|
41 ## @result{} '[-0.3333+0.14i;0.3333-0.14i]' |
|
42 ## mat2str( [ -1/3 +i/7; 1/3 -i/7 ], [4 2] ) |
|
43 ## @result{} '[-0.3333+0i,0+0.14i;0.3333+0i,-0-0.14i]' |
|
44 ## mat2str( int16([1 -1]), 'class') |
|
45 ## @result{} 'int16([1,-1])' |
|
46 ## @end group |
|
47 ## @end example |
|
48 ## |
|
49 ## @seealso{sprintf, int2str} |
|
50 ## @end deftypefn |
|
51 |
|
52 function s = mat2str(x,n,cls) |
|
53 |
|
54 if (nargin < 2 || isempty(n)) |
|
55 n = 17; # default precision |
|
56 endif |
|
57 |
|
58 if (nargin < 3) |
|
59 if (ischar(n)) |
|
60 cls = n; |
|
61 n = 17; |
|
62 else |
|
63 cls = ''; |
|
64 endif |
|
65 endif |
|
66 |
|
67 if (nargin < 1 || nargin > 3 || ischar(x) || isstruct(x) || |
|
68 ischar(n) || isstruct(n) || isstruct(cls)) |
|
69 usage ("mat2str"); |
|
70 endif |
|
71 |
|
72 if (ndims (x) > 2) |
|
73 error ("mat2str: x must be two dimensional"); |
|
74 endif |
|
75 |
|
76 if (!(COMPLEX = is_complex(x))) |
|
77 FMT = sprintf("%%.%dg", n(1)); |
|
78 else |
|
79 if (length(n) == 1 ) |
|
80 n = [n, n]; |
|
81 endif |
|
82 FMT = sprintf("%%.%dg%%+.%dgi", n(1), n(2)); |
|
83 endif |
|
84 |
|
85 [nr, nc] = size(x); |
|
86 |
|
87 if (nr*nc == 0) # empty .. only print brackets |
|
88 s = "[]"; |
|
89 elseif (nr*nc == 1) # scalar x .. don't print brackets |
|
90 if (!COMPLEX) |
|
91 s = sprintf( FMT, x ); |
|
92 else |
|
93 s = sprintf( FMT, real(x), imag(x) ); |
|
94 endif |
|
95 else # non-scalar x .. print brackets |
|
96 FMT = [FMT, ',']; |
|
97 if (!COMPLEX) |
|
98 s = sprintf( FMT, x.' ); |
|
99 else |
|
100 x = x.'; |
|
101 s = sprintf( FMT, [ real(x(:))'; imag(x(:))' ] ); |
|
102 endif |
|
103 |
|
104 s = ["[", s]; |
|
105 s (length(s)) = "]"; |
|
106 IND = find(s == ","); |
|
107 s (IND(nc:nc:length(IND)) ) = ";"; |
|
108 endif |
|
109 |
|
110 if (strcmp ("class", cls)) |
|
111 s = [class(x), "(", s, ")"] |
|
112 endif |
|
113 endfunction |