Mercurial > hg > octave-lyh
comparison scripts/strings/mat2str.m @ 11485:571bfa4fc295
mat2str: handle logical arguments
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 12 Jan 2011 01:15:23 -0500 |
parents | be55736a0783 |
children | fd0a3ac60b0e |
comparison
equal
deleted
inserted
replaced
11484:cda6044447a0 | 11485:571bfa4fc295 |
---|---|
66 else | 66 else |
67 cls = ""; | 67 cls = ""; |
68 endif | 68 endif |
69 endif | 69 endif |
70 | 70 |
71 if (nargin < 1 || nargin > 3 || ! isnumeric (x)) | 71 if (nargin < 1 || nargin > 3 || ! (isnumeric (x) || islogical (x))) |
72 print_usage (); | 72 print_usage (); |
73 endif | 73 endif |
74 | 74 |
75 if (ndims (x) > 2) | 75 if (ndims (x) > 2) |
76 error ("mat2str: X must be two dimensional"); | 76 error ("mat2str: X must be two dimensional"); |
77 endif | 77 endif |
78 | 78 |
79 x_islogical = islogical (x); | |
79 x_iscomplex = iscomplex (x); | 80 x_iscomplex = iscomplex (x); |
80 | 81 |
81 if (! x_iscomplex) | 82 if (x_iscomplex) |
82 fmt = sprintf ("%%.%dg", n(1)); | |
83 else | |
84 if (length (n) == 1) | 83 if (length (n) == 1) |
85 n = [n, n]; | 84 n = [n, n]; |
86 endif | 85 endif |
87 fmt = sprintf ("%%.%dg%%+.%dgi", n(1), n(2)); | 86 fmt = sprintf ("%%.%dg%%+.%dgi", n(1), n(2)); |
87 elseif (x_islogical) | |
88 v = {"false", "true"}; | |
89 fmt = "%s"; | |
90 else | |
91 fmt = sprintf ("%%.%dg", n(1)); | |
88 endif | 92 endif |
89 | 93 |
90 nel = numel (x); | 94 nel = numel (x); |
91 | 95 |
92 if (nel == 0) | 96 if (nel == 0) |
93 ## Empty, only print brackets | 97 ## Empty, only print brackets |
94 s = "[]"; | 98 s = "[]"; |
95 elseif (nel == 1) | 99 elseif (nel == 1) |
96 ## Scalar X, don't print brackets | 100 ## Scalar X, don't print brackets |
97 if (! x_iscomplex) | 101 if (x_iscomplex) |
102 s = sprintf (fmt, real (x), imag (x)); | |
103 elseif (x_islogical) | |
104 s = v{x+1}; | |
105 else | |
98 s = sprintf (fmt, x); | 106 s = sprintf (fmt, x); |
99 else | |
100 s = sprintf (fmt, real (x), imag (x)); | |
101 endif | 107 endif |
102 else | 108 else |
103 ## Non-scalar X, print brackets | 109 ## Non-scalar X, print brackets |
104 fmt = [fmt, ","]; | 110 fmt = cstrcat (fmt, ","); |
105 if (! x_iscomplex) | 111 if (x_iscomplex) |
106 s = sprintf (fmt, x.'); | |
107 else | |
108 t = x.'; | 112 t = x.'; |
109 s = sprintf (fmt, [real(t(:))'; imag(t(:))']); | 113 s = sprintf (fmt, [real(t(:))'; imag(t(:))']); |
114 elseif (x_islogical) | |
115 t = v(x+1); | |
116 s = cstrcat (sprintf (fmt, t{:})); | |
117 else | |
118 s = sprintf (fmt, x.'); | |
110 endif | 119 endif |
111 | 120 |
112 s = ["[", s]; | 121 s = cstrcat ("[", s); |
113 s(end) = "]"; | 122 s(end) = "]"; |
114 ind = find (s == ","); | 123 ind = find (s == ","); |
115 nc = columns (x); | 124 nc = columns (x); |
116 s(ind(nc:nc:end)) = ";"; | 125 s(ind(nc:nc:end)) = ";"; |
117 endif | 126 endif |
118 | 127 |
119 if (strcmp ("class", cls)) | 128 if (strcmp ("class", cls)) |
120 s = [class(x), "(", s, ")"]; | 129 s = cstrcat (class(x), "(", s, ")"); |
121 endif | 130 endif |
122 endfunction | 131 endfunction |
123 | 132 |
124 %!assert (mat2str ([-1/3 + i/7; 1/3 - i/7], [4 2]), "[-0.3333+0.14i;0.3333-0.14i]") | 133 %!assert (mat2str ([-1/3 + i/7; 1/3 - i/7], [4 2]), "[-0.3333+0.14i;0.3333-0.14i]") |
125 %!assert (mat2str ([-1/3 +i/7; 1/3 -i/7], [4 2]), "[-0.3333+0i,0+0.14i;0.3333+0i,-0-0.14i]") | 134 %!assert (mat2str ([-1/3 +i/7; 1/3 -i/7], [4 2]), "[-0.3333+0i,0+0.14i;0.3333+0i,-0-0.14i]") |
126 %!assert (mat2str (int16 ([1 -1]), 'class'), "int16([1,-1])") | 135 %!assert (mat2str (int16 ([1 -1]), 'class'), "int16([1,-1])") |
127 | 136 |
137 %!assert (mat2str (true), "true"); | |
138 %!assert (mat2str (false), "false"); | |
139 %!assert (mat2str (logical (eye (2))), "[true,false;false,true]"); |