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 ## |
7001
|
24 ## Format real/complex numerical matrices as strings. This function |
5837
|
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 |
5838
|
52 function s = mat2str (x, n, cls) |
5837
|
53 |
5838
|
54 if (nargin < 2 || isempty (n)) |
|
55 ## Default precision |
|
56 n = 17; |
5837
|
57 endif |
|
58 |
|
59 if (nargin < 3) |
5838
|
60 if (ischar (n)) |
5837
|
61 cls = n; |
|
62 n = 17; |
|
63 else |
5838
|
64 cls = ""; |
5837
|
65 endif |
|
66 endif |
|
67 |
5838
|
68 if (nargin < 1 || nargin > 3 || ! isnumeric (x)) |
|
69 print_usage (); |
5837
|
70 endif |
|
71 |
|
72 if (ndims (x) > 2) |
5838
|
73 error ("mat2str: X must be two dimensional"); |
5837
|
74 endif |
|
75 |
5838
|
76 x_is_complex = is_complex (x); |
|
77 |
|
78 if (! x_is_complex) |
|
79 fmt = sprintf ("%%.%dg", n(1)); |
5837
|
80 else |
5838
|
81 if (length (n) == 1 ) |
5837
|
82 n = [n, n]; |
|
83 endif |
5838
|
84 fmt = sprintf ("%%.%dg%%+.%dgi", n(1), n(2)); |
5837
|
85 endif |
|
86 |
5838
|
87 nel = numel (x); |
5837
|
88 |
5838
|
89 if (nel == 0) |
|
90 ## Empty, only print brackets |
5837
|
91 s = "[]"; |
5838
|
92 elseif (nel == 1) |
|
93 ## Scalar X, don't print brackets |
|
94 if (! x_is_complex) |
|
95 s = sprintf (fmt, x); |
5837
|
96 else |
5838
|
97 s = sprintf (fmt, real (x), imag (x)); |
5837
|
98 endif |
5838
|
99 else |
|
100 ## Non-scalar X, print brackets |
|
101 fmt = [fmt, ","]; |
|
102 if (! x_is_complex) |
|
103 s = sprintf (fmt, x.'); |
5837
|
104 else |
|
105 x = x.'; |
5838
|
106 s = sprintf (fmt, [real(x(:))'; imag(x(:))']); |
5837
|
107 endif |
|
108 |
|
109 s = ["[", s]; |
5838
|
110 s(end) = "]"; |
|
111 ind = find (s == ","); |
5946
|
112 nc = columns (x); |
5838
|
113 s(ind(nc:nc:end)) = ";"; |
5837
|
114 endif |
|
115 |
|
116 if (strcmp ("class", cls)) |
|
117 s = [class(x), "(", s, ")"] |
|
118 endif |
|
119 endfunction |