1 ## Copyright (C) 2002-2012 Rolf Fabian
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {@var{s} =} mat2str (@var{x}, @var{n})
21 ## @deftypefnx {Function File} {@var{s} =} mat2str (@var{x}, @var{n}, "class")
22 ## Format real, complex, and logical matrices as strings. The
23 ## returned string may be used to reconstruct the original matrix by using
24 ## the @code{eval} function.
26 ## The precision of the values is given by @var{n}. If @var{n} is a
27 ## scalar then both real and imaginary parts of the matrix are printed
28 ## to the same precision. Otherwise @code{@var{n}(1)} defines the
29 ## precision of the real part and @code{@var{n}(2)} defines the
30 ## precision of the imaginary part. The default for @var{n} is 15.
32 ## If the argument "class" is given then the class of @var{x} is
33 ## included in the string in such a way that @code{eval} will result in the
34 ## construction of a matrix of the same class.
38 ## mat2str ([ -1/3 + i/7; 1/3 - i/7 ], [4 2])
39 ## @result{} "[-0.3333+0.14i;0.3333-0.14i]"
41 ## mat2str ([ -1/3 +i/7; 1/3 -i/7 ], [4 2])
42 ## @result{} "[-0.3333+0i 0+0.14i;0.3333+0i -0-0.14i]"
44 ## mat2str (int16([1 -1]), "class")
45 ## @result{} "int16([1 -1])"
47 ## mat2str (logical (eye (2)))
48 ## @result{} "[true false;false true]"
50 ## isequal (x, eval (mat2str (x)))
55 ## @seealso{sprintf, num2str, int2str}
58 ## Author: Rolf Fabian <fabian@tu-cottbus.de>
60 function s = mat2str (x, n = 15, cls = "")
62 if (nargin < 1 || nargin > 3 || ! (isnumeric (x) || islogical (x)))
64 elseif (ndims (x) > 2)
65 error ("mat2str: X must be two dimensional");
68 if (nargin == 2 && ischar (n))
72 n = 15; # Default precision
75 x_islogical = islogical (x);
76 x_iscomplex = iscomplex (x);
82 fmt = sprintf ("%%.%dg%%+.%dgi", n(1), n(2));
84 v = {"false", "true"};
87 fmt = sprintf ("%%.%dg", n(1));
93 ## Empty, only print brackets
96 ## Scalar X, don't print brackets
98 s = sprintf (fmt, real (x), imag (x));
102 s = sprintf (fmt, x);
105 ## Non-scalar X, print brackets
106 fmt = cstrcat (fmt, " ");
109 s = sprintf (fmt, [real(t(:))'; imag(t(:))']);
112 s = cstrcat (sprintf (fmt, t{:}));
114 s = sprintf (fmt, x.');
117 s = cstrcat ("[", s);
119 idx = strfind (s, " ");
121 s(idx(nc:nc:end)) = ";";
124 if (strcmp ("class", cls))
125 s = cstrcat (class (x), "(", s, ")");
131 %!assert (mat2str (0.7), "0.7");
132 %!assert (mat2str (pi), "3.14159265358979");
133 %!assert (mat2str (pi, 5), "3.1416");
134 %!assert (mat2str (single (pi), 5, "class"), "single(3.1416)");
135 %!assert (mat2str ([-1/3 + i/7; 1/3 - i/7], [4 2]), "[-0.3333+0.14i;0.3333-0.14i]")
136 %!assert (mat2str ([-1/3 +i/7; 1/3 -i/7], [4 2]), "[-0.3333+0i 0+0.14i;0.3333+0i -0-0.14i]")
137 %!assert (mat2str (int16 ([1 -1]), 'class'), "int16([1 -1])")
138 %!assert (mat2str (true), "true");
139 %!assert (mat2str (false), "false");
140 %!assert (mat2str (logical (eye (2))), "[true false;false true]");
142 %% Test input validation
144 %!error mat2str (1,2,3,4)
145 %!error mat2str (["Hello"])
146 %!error mat2str (ones(3,3,2))