changeset 25737:799dcddaf158

strmatch.m: Accept a single cellstr as first input for Matlab compatibility (bug #54432) * strmatch.m: Check for a cellstr as first input and convert to a string with char(). Add BIST test for bug #54432. Add BIST for new input validation test that an input cellstr contains just one string.
author Rik <rik@octave.org>
date Fri, 03 Aug 2018 09:03:47 -0700
parents 138f91fb2883
children 8cdaef4c6d44
files scripts/strings/strmatch.m
diffstat 1 files changed, 9 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/strings/strmatch.m	Fri Aug 03 09:00:49 2018 -0700
+++ b/scripts/strings/strmatch.m	Fri Aug 03 09:03:47 2018 -0700
@@ -60,7 +60,12 @@
     print_usage ();
   endif
 
-  if (! ischar (s) || (! isempty (s) && ! isvector (s)))
+  if (iscellstr (s))
+    if (! isscalar (s))
+      error ("strmatch: a cell array S must contain only one string");
+    endif
+    s = char (s);
+  elseif (! ischar (s) || (! isempty (s) && ! isrow (s)))
     error ("strmatch: S must be a string");
   elseif (! (ischar (A) || iscellstr (A)))
     error ("strmatch: A must be a string or cell array of strings");
@@ -119,11 +124,14 @@
 %! assert (strmatch (" ", "   "), 1);
 %! assert (strmatch ("  ", " "), []);
 %! assert (strmatch ("  ", "  "), 1);
+%!test <*54432>
+%! assert (strmatch ({"a"}, {"aaa", "bab", "bbb"}), 1);
 
 ## Test input validation
 %!error <Invalid call to strmatch> strmatch ()
 %!error <Invalid call to strmatch> strmatch ("a")
 %!error <Invalid call to strmatch> strmatch ("a", "aaa", "exact", 1)
+%!error <S must contain only one string> strmatch ({"a", "b"}, "aaa")
 %!error <S must be a string> strmatch (1, "aaa")
 %!error <S must be a string> strmatch (char ("a", "bb"), "aaa")
 %!error <A must be a string> strmatch ("a", 1)