changeset 28120:de2c73ae450f

allow data type argument for true and false functions * data.cc (fill_matrix (const octave_value_list&, bool, const char*)): Accept data type as final argument, required to be "logical" if present.
author John W. Eaton <jwe@octave.org>
date Wed, 19 Feb 2020 18:42:00 -0500
parents 34a3d005e2ad
children 865f8ddb2c78
files libinterp/corefcn/data.cc
diffstat 1 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/data.cc	Wed Feb 19 14:53:41 2020 -0800
+++ b/libinterp/corefcn/data.cc	Wed Feb 19 18:42:00 2020 -0500
@@ -4248,6 +4248,18 @@
 
   dim_vector dims (1, 1);
 
+  // The TYPE argument is required to be "logical" if present.  This
+  // feature appears to be undocumented in Matlab.
+
+  if (nargin > 0 && args(nargin-1).is_string ())
+    {
+      std::string nm = args(nargin-1).string_value ();
+      nargin--;
+
+      if (oct_data_conv::string_to_data_type (nm) != oct_data_conv::dt_logical)
+        error ("%s: invalid data type '%s'", fcn, nm.c_str ());
+    }
+
   switch (nargin)
     {
     case 0:
@@ -4908,6 +4920,12 @@
   return fill_matrix (args, false, "false");
 }
 
+/*
+%!assert (false (2, 3), logical (zeros (2, 3)))
+%!assert (false (2, 3, "logical"), logical (zeros (2, 3)))
+%!error false (2, 3, "double")
+*/
+
 DEFUN (true, args, ,
        doc: /* -*- texinfo -*-
 @deftypefn  {} {} true (@var{x})
@@ -4926,6 +4944,12 @@
   return fill_matrix (args, true, "true");
 }
 
+/*
+%!assert (true (2, 3), logical (ones (2, 3)))
+%!assert (true (2, 3, "logical"), logical (ones (2, 3)))
+%!error true (2, 3, "double")
+*/
+
 template <typename MT>
 octave_value
 identity_matrix (int nr, int nc)