comparison scripts/general/num2str.m @ 20301:f357e076776f

num2str.m: Fixed two Matlab compatibility issues (bug #45174, bug #44864). * num2str.m: Don't use repmat to modify user-supplied conversion format if it contains more than one conversion specifier or if the string (%s) or character (%c) conversions are present.
author Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
date Wed, 17 Jun 2015 16:52:09 +0200
parents ea5fdb2ae637
children 4bb41929286b
comparison
equal deleted inserted replaced
20300:01895a6acdde 20301:f357e076776f
114 ## Logical input 114 ## Logical input
115 fmt = "%3d"; 115 fmt = "%3d";
116 endif 116 endif
117 endif 117 endif
118 fmt = do_string_escapes (fmt); # required now that '\n' is interpreted. 118 fmt = do_string_escapes (fmt); # required now that '\n' is interpreted.
119 fmt = [deblank(repmat (fmt, 1, columns (x))), "\n"];
120 nd = ndims (x); 119 nd = ndims (x);
121 tmp = sprintf (fmt, permute (x, [2, 1, 3:nd])); 120 nc = columns (x);
122 retval = strtrim (char (ostrsplit (tmp(1:end-1), "\n"))); 121 x = permute (x, [2, 1, 3:nd]);
122 if (! (sum (fmt == "%") > 1 || any (strcmp (fmt, {"%s", "%c"}))))
123 fmt = [deblank(repmat (fmt, 1, nc)), "\n"];
124 endif
125 tmp = sprintf (fmt, x);
126 retval = strtrim (char (ostrsplit (tmp, "\n", true)));
123 else # Complex matrix input 127 else # Complex matrix input
124 if (nargin == 2) 128 if (nargin == 2)
125 if (ischar (arg)) 129 if (ischar (arg))
126 fmt = [arg "%-+" arg(2:end) "i"]; 130 fmt = [arg "%-+" arg(2:end) "i"];
127 elseif (isnumeric (arg) && isscalar (arg) && arg >= 0 && arg == fix (arg)) 131 elseif (isnumeric (arg) && isscalar (arg) && arg >= 0 && arg == fix (arg))
206 %! assert (num2str (1e23), "100000000000000000000000"); 210 %! assert (num2str (1e23), "100000000000000000000000");
207 211
208 ## Test for bug #44864, extra rows generated from newlines in format 212 ## Test for bug #44864, extra rows generated from newlines in format
209 %!assert (rows (num2str (magic (3), '%3d %3d %3d\n')), 3) 213 %!assert (rows (num2str (magic (3), '%3d %3d %3d\n')), 3)
210 214
215 ## Test for bug #45174
216 %!assert (num2str ([65 66 67], '%s'), "ABC")
217
211 %!error num2str () 218 %!error num2str ()
212 %!error num2str (1, 2, 3) 219 %!error num2str (1, 2, 3)
213 %!error <X must be a numeric> num2str ({1}) 220 %!error <X must be a numeric> num2str ({1})
214 %!error <PRECISION must be a scalar integer> num2str (1, {1}) 221 %!error <PRECISION must be a scalar integer> num2str (1, {1})
215 %!error <PRECISION must be a scalar integer> num2str (1, ones (2)) 222 %!error <PRECISION must be a scalar integer> num2str (1, ones (2))