comparison scripts/strings/mat2str.m @ 7812:c25094267486

strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
author kimhanse@gmail.com
date Wed, 21 May 2008 12:26:57 +0200
parents a1dbe9d80eee
children 502e58a0d44f
comparison
equal deleted inserted replaced
7811:12c8b195508f 7812:c25094267486
72 72
73 if (ndims (x) > 2) 73 if (ndims (x) > 2)
74 error ("mat2str: X must be two dimensional"); 74 error ("mat2str: X must be two dimensional");
75 endif 75 endif
76 76
77 x_is_complex = is_complex (x); 77 x_iscomplex = iscomplex (x);
78 78
79 if (! x_is_complex) 79 if (! x_iscomplex)
80 fmt = sprintf ("%%.%dg", n(1)); 80 fmt = sprintf ("%%.%dg", n(1));
81 else 81 else
82 if (length (n) == 1 ) 82 if (length (n) == 1 )
83 n = [n, n]; 83 n = [n, n];
84 endif 84 endif
90 if (nel == 0) 90 if (nel == 0)
91 ## Empty, only print brackets 91 ## Empty, only print brackets
92 s = "[]"; 92 s = "[]";
93 elseif (nel == 1) 93 elseif (nel == 1)
94 ## Scalar X, don't print brackets 94 ## Scalar X, don't print brackets
95 if (! x_is_complex) 95 if (! x_iscomplex)
96 s = sprintf (fmt, x); 96 s = sprintf (fmt, x);
97 else 97 else
98 s = sprintf (fmt, real (x), imag (x)); 98 s = sprintf (fmt, real (x), imag (x));
99 endif 99 endif
100 else 100 else
101 ## Non-scalar X, print brackets 101 ## Non-scalar X, print brackets
102 fmt = [fmt, ","]; 102 fmt = [fmt, ","];
103 if (! x_is_complex) 103 if (! x_iscomplex)
104 s = sprintf (fmt, x.'); 104 s = sprintf (fmt, x.');
105 else 105 else
106 x = x.'; 106 t = x.';
107 s = sprintf (fmt, [real(x(:))'; imag(x(:))']); 107 s = sprintf (fmt, [real(t(:))'; imag(t(:))']);
108 endif 108 endif
109 109
110 s = ["[", s]; 110 s = ["[", s];
111 s(end) = "]"; 111 s(end) = "]";
112 ind = find (s == ","); 112 ind = find (s == ",");
113 nc = columns (x); 113 nc = columns (x);
114 s(ind(nc:nc:end)) = ";"; 114 s(ind(nc:nc:end)) = ";";
115 endif 115 endif
116 116
117 if (strcmp ("class", cls)) 117 if (strcmp ("class", cls))
118 s = [class(x), "(", s, ")"] 118 s = [class(x), "(", s, ")"];
119 endif 119 endif
120 endfunction 120 endfunction
121
122 %!assert (mat2str ([-1/3 + i/7; 1/3 - i/7], [4 2]), "[-0.3333+0.14i;0.3333-0.14i]")
123 %!assert (mat2str ([-1/3 +i/7; 1/3 -i/7], [4 2]), "[-0.3333+0i,0+0.14i;0.3333+0i,-0-0.14i]")
124 %!assert (mat2str (int16 ([1 -1]), 'class'), "int16([1,-1])")
125