changeset 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 01895a6acdde
children ce8fda51d236
files scripts/general/num2str.m
diffstat 1 files changed, 10 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/num2str.m	Mon Jun 15 17:52:27 2015 -0400
+++ b/scripts/general/num2str.m	Wed Jun 17 16:52:09 2015 +0200
@@ -116,10 +116,14 @@
       endif
     endif
     fmt = do_string_escapes (fmt);  # required now that '\n' is interpreted.
-    fmt = [deblank(repmat (fmt, 1, columns (x))), "\n"];
     nd = ndims (x);
-    tmp = sprintf (fmt, permute (x, [2, 1, 3:nd]));
-    retval = strtrim (char (ostrsplit (tmp(1:end-1), "\n")));
+    nc = columns (x);
+    x  = permute (x, [2, 1, 3:nd]);
+    if (! (sum (fmt == "%") > 1 || any (strcmp (fmt, {"%s", "%c"}))))
+      fmt = [deblank(repmat (fmt, 1, nc)), "\n"];
+    endif
+    tmp    = sprintf (fmt, x);
+    retval = strtrim (char (ostrsplit (tmp, "\n", true)));
   else   # Complex matrix input
     if (nargin == 2)
       if (ischar (arg))
@@ -208,6 +212,9 @@
 ## Test for bug #44864, extra rows generated from newlines in format
 %!assert (rows (num2str (magic (3), '%3d %3d %3d\n')), 3)
 
+## Test for bug #45174
+%!assert (num2str ([65 66 67], '%s'), "ABC")
+
 %!error num2str ()
 %!error num2str (1, 2, 3)
 %!error <X must be a numeric> num2str ({1})