# HG changeset patch # User Rik # Date 1648595721 25200 # Node ID 1bddc0ddea74e0c779e88d79f76d293ee7691abb # Parent b7826af05108f984517b9720575b515ecbe6b899 cylinder.m: Accept scalar radius input for Matlab compatibility. * NEWS: Announce change in Matlab compatibility section. * cylinder.m: Check for scalar R input and expand to 2-term form if necessary. Add input validation for input R. Add BIST tests for input validation. Rename output variables xx,yy,zz to x,y,z to match documentation. Rename internal variables x,y,z to xx,yy,zz. diff -r b7826af05108 -r 1bddc0ddea74 scripts/plot/draw/cylinder.m --- a/scripts/plot/draw/cylinder.m Tue Mar 29 16:09:52 2022 -0700 +++ b/scripts/plot/draw/cylinder.m Tue Mar 29 16:15:21 2022 -0700 @@ -57,7 +57,7 @@ ## @seealso{ellipsoid, rectangle, sphere} ## @end deftypefn -function [xx, yy, zz] = cylinder (varargin) +function [x, y, z] = cylinder (varargin) [hax, args, nargs] = __plt_get_axis_arg__ ("cylinder", varargin{:}); @@ -74,21 +74,24 @@ n = args{2}; endif - if (length (r) < 2) - error ("cylinder: length (R) must be larger than 2"); + if (! isvector (r)) + error ("cylinder: R must be a scalar or vector"); + endif + if (isscalar (r)) + r .*= [1 1]; # expand single radius specification to required 2-term form endif phi = linspace (0, 2*pi, n+1); idx = 1:length (r); [phi, idx] = meshgrid (phi, idx); - z = (idx - 1) / (length (r) - 1); + zz = (idx - 1) / (length (r) - 1); r = r(idx); - [x, y] = pol2cart (phi, r); + [xx, yy] = pol2cart (phi, r); if (nargout > 0) - xx = x; - yy = y; - zz = z; + x = xx; + y = yy; + z = zz; else oldfig = []; if (! isempty (hax)) @@ -96,7 +99,7 @@ endif unwind_protect hax = newplot (hax); - surf (x, y, z); + surf (xx, yy, zz); unwind_protect_cleanup if (! isempty (oldfig)) set (0, "currentfigure", oldfig); @@ -113,3 +116,8 @@ %! [x, y, z] = cylinder (10:-1:0, 50); %! surf (x, y, z); %! title ("cylinder() with linearly shrinking radius produces a cone"); + +## Test input validation +%!error cylinder (1,2,3) +%!error cylinder ([]) +%!error cylinder (ones (2,2))