changeset 16987:c9346014fed2

Validate inputs are real for mesh, meshc, meshz functions. Rename internal helper variables for greater clarity. * scripts/plot/mesh.m: Validate inputs are real. Use htmp for temporary handle. * scripts/plot/meshc.m: Validate inputs are real. Use htmp for temporary handle. * scripts/plot/meshz.m: Validate inputs are real. Use htmp for temporary handle. Use cellfun to replace for loop in input validation. Use oldax for saved axes handle.
author Rik <rik@octave.org>
date Sun, 14 Jul 2013 14:12:33 -0700
parents 997efb8d0b19
children df66488b8d43
files scripts/plot/mesh.m scripts/plot/meshc.m scripts/plot/meshz.m
diffstat 3 files changed, 55 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/mesh.m
+++ b/scripts/plot/mesh.m
@@ -42,14 +42,18 @@
 
 function h = mesh (varargin)
 
+  if (! all (cellfun ("isreal", varargin)))
+    error ("mesh: X, Y, Z, C arguments must be real");
+  endif
+
   newplot ();
 
-  tmp = surface (varargin{:});
+  htmp = surface (varargin{:});
 
-  ax = get (tmp, "parent");
+  ax = get (htmp, "parent");
 
-  set (tmp, "facecolor", "w");
-  set (tmp, "edgecolor", "flat");
+  set (htmp, "facecolor", "w");
+  set (htmp, "edgecolor", "flat");
 
   if (! ishold ())
     set (ax, "view", [-37.5, 30],
@@ -57,7 +61,7 @@
   endif
 
   if (nargout > 0)
-    h = tmp;
+    h = htmp;
   endif
 
 endfunction
@@ -68,8 +72,8 @@
 %! x = logspace (0,1,11);
 %! z = x'*x;
 %! mesh (x, x, z, z.^2);
-%! xlabel xlabel;
-%! ylabel ylabel;
+%! xlabel 'X-axis';
+%! ylabel 'Y-axis';
 %! zlabel 'linear scale';
 
 %!demo
@@ -78,8 +82,8 @@
 %! z = x'*x;
 %! mesh (x, x, z, z.^2);
 %! set (gca, 'zscale', 'log');
-%! xlabel xlabel;
-%! ylabel ylabel;
+%! xlabel 'X-axis';
+%! ylabel 'Y-axis';
 %! zlabel 'log scale';
 %! if (strcmp (get (gcf, '__graphics_toolkit__'), 'gnuplot'))
 %!   title ({'Gnuplot: mesh color is wrong', 'This a Gnuplot bug'});
--- a/scripts/plot/meshc.m
+++ b/scripts/plot/meshc.m
@@ -29,18 +29,21 @@
 
 function h = meshc (varargin)
 
+  if (! all (cellfun ("isreal", varargin)))
+    error ("meshc: X, Y, Z, C arguments must be real");
+  endif
+
   newplot ();
 
-  tmp = surface (varargin{:});
+  htmp = surface (varargin{:});
 
-  ax = get (tmp, "parent");
+  ax = get (htmp, "parent");
 
-  set (tmp, "facecolor", "w");
-  set (tmp, "edgecolor", "flat");
+  set (htmp, "facecolor", "w");
+  set (htmp, "edgecolor", "flat");
   ## FIXME - gnuplot does not support a filled surface and a
-  ## non-filled contour. 3D filled patches are also not supported.
-  ## Thus, the facecolor will be transparent for the gnuplot
-  ## backend.
+  ## non-filled contour.  3D filled patches are also not supported.
+  ## Thus, the facecolor will be transparent for the gnuplot backend.
 
   if (! ishold ())
     set (ax, "view", [-37.5, 30],
@@ -50,12 +53,12 @@
   drawnow ();
   zmin = get (ax, "zlim")(1);
 
-  [c, tmp2] = __contour__ (ax, zmin, varargin{:});
+  [~, htmp2] = __contour__ (ax, zmin, varargin{:});
 
-  tmp = [tmp; tmp2];
+  htmp = [htmp; htmp2];
 
   if (nargout > 0)
-    h = tmp;
+    h = htmp;
   endif
 
 endfunction
--- a/scripts/plot/meshz.m
+++ b/scripts/plot/meshz.m
@@ -27,24 +27,26 @@
 ## @seealso{meshgrid, mesh, contour}
 ## @end deftypefn
 
-function retval = meshz (varargin)
-
-  [h, varargin, nargin] = __plt_get_axis_arg__ ("meshz", varargin{:});
+function h = meshz (varargin)
 
-  ioff = nargin + 1;
-  for i = 1:nargin
-    if (ischar (varargin{i}))
-      ioff = i;
-      break;
-    endif
-  endfor
-
-  ## Bundle C matrix back into varargin
-  if (ioff == 3 || ioff == 5)
-    ioff --;
+  if (! all (cellfun ("isreal", varargin)))
+    error ("meshz: X, Y, Z, C arguments must be real");
   endif
 
-  if (ioff == 2)
+  [ax, varargin, nargin] = __plt_get_axis_arg__ ("meshz", varargin{:});
+
+  ## Find where property/value pairs start
+  charidx = find (cellfun ("isclass", varargin, "char"), 1);
+
+  if (isempty (charidx))
+    if (nargin == 2 || nargin == 4) 
+      charidx = nargin;   # bundle C matrix back into varargin 
+    else
+      charidx = nargin + 1;
+    endif
+  endif
+
+  if (charidx == 2)
     z = varargin{1};
     [m, n] = size (z);
     x = 1:n;
@@ -55,17 +57,16 @@
     z = varargin{3};
   endif
 
-
   if (isvector (x) && isvector (y))
     x = [x(1), x(:).', x(end)];
     y = [y(1); y(:); y(end)];
   else
-    x = [x(1, 1), x(1, :), x(1, end);
-         x(:, 1), x, x(:, end);
-         x(end, 1), x(end, :), x(end, end)];
-    y = [y(1, 1), y(1, :), y(1, end);
-         y(:, 1), y, y(:, end);
-         y(end, 1), y(end, :), y(end, end)];
+    x = [x(1,1), x(1,:), x(1,end);
+         x(:,1), x, x(:,end);
+         x(end,1), x(end,:), x(end,end)];
+    y = [y(1,1), y(1,:), y(1,end);
+         y(:,1), y, y(:,end);
+         y(end,1), y(end,:), y(end,end)];
   endif
 
   zref = min (z(isfinite (z)));
@@ -73,16 +74,17 @@
        zref .* ones(rows(z), 1), z, zref .* ones(rows(z), 1);
        zref.* ones(1, columns(z) + 2)];
 
-  oldh = gca ();
+  oldax = gca ();
   unwind_protect
-    axes (h);
-    tmp = mesh (x, y, z, varargin{ioff:end});
+    axes (ax);
+    htmp = mesh (x, y, z, varargin{charidx:end});
   unwind_protect_cleanup
-    axes (oldh);
+    axes (oldax);
   end_unwind_protect
 
   if (nargout > 0)
-    retval = tmp;
+    h = htmp;
   endif
 
 endfunction
+