# HG changeset patch # User Rik # Date 1391495532 28800 # Node ID 07ca8b9f8cab4ca5a29c52efaa9249613a943bd3 # Parent cb377af34c00e65ac7a01fbe94399bdd72251d70 surface.m: Simplify input argument processing. * surface.m (__surface__): Use cellfun call rather than loop to find first string argument. Use switch statement instead of long if/elseif tree. diff -r cb377af34c00 -r 07ca8b9f8cab scripts/plot/draw/surface.m --- a/scripts/plot/draw/surface.m Sun Feb 02 22:21:45 2014 -0800 +++ b/scripts/plot/draw/surface.m Mon Feb 03 22:32:12 2014 -0800 @@ -73,109 +73,110 @@ h = 0; bad_usage = false; - firststring = nargin; - for i = 1 : (nargin - 1) - if (ischar (varargin{i})) - firststring = i; - break; - endif - endfor + firststring = find (cellfun ("isclass", varargin, "char"), 1); + if (isempty (firststring)) + firststring = nargin; + endif - if (firststring > 5) - bad_usage = true; - return; - elseif (firststring == 5) - x = varargin{1}; - y = varargin{2}; - z = varargin{3}; - c = varargin{4}; + switch (firststring) + case 5 + x = varargin{1}; + y = varargin{2}; + z = varargin{3}; + c = varargin{4}; - if (iscomplex (x) || iscomplex (y) || iscomplex (z) || iscomplex (c)) - error ("mesh: X, Y, Z, C arguments must be real"); - endif + if (iscomplex (x) || iscomplex (y) || iscomplex (z) || iscomplex (c)) + error ("mesh: X, Y, Z, C arguments must be real"); + endif + + [z_nr, z_nc] = size (z); + [c_nr, c_nc, c_np] = size (c); + if (! (z_nr == c_nr && z_nc == c_nc && (c_np == 1 || c_np == 3))) + error ("surface: Z and C must have the same size"); + endif - [z_nr, z_nc] = size (z); - [c_nr, c_nc, c_np] = size (c); - if (! (z_nr == c_nr && z_nc == c_nc && (c_np == 1 || c_np == 3))) - error ("surface: Z and C must have the same size"); - endif - - if (isvector (x) && isvector (y) && ismatrix (z)) - if (rows (z) == length (y) && columns (z) == length (x)) - x = x(:)'; - y = y(:); + if (isvector (x) && isvector (y) && ismatrix (z)) + if (rows (z) == length (y) && columns (z) == length (x)) + x = x(:)'; + y = y(:); + else + error ("surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)"); + endif + elseif (ismatrix (x) && ismatrix (y) && ismatrix (z)) + if (! size_equal (x, y, z)) + error ("surface: X, Y, and Z must have the same dimensions"); + endif else - error ("surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)"); + error ("surface: X and Y must be vectors and Z must be a matrix"); endif - elseif (ismatrix (x) && ismatrix (y) && ismatrix (z)) - if (! size_equal (x, y, z)) - error ("surface: X, Y, and Z must have the same dimensions"); + + case 4 + x = varargin{1}; + y = varargin{2}; + z = varargin{3}; + c = z; + + if (iscomplex (x) || iscomplex (y) || iscomplex (z)) + error ("mesh: X, Y, Z arguments must be real"); endif - else - error ("surface: X and Y must be vectors and Z must be a matrix"); - endif - elseif (firststring == 4) - x = varargin{1}; - y = varargin{2}; - z = varargin{3}; - c = z; - - if (iscomplex (x) || iscomplex (y) || iscomplex (z)) - error ("mesh: X, Y, Z arguments must be real"); - endif - if (isvector (x) && isvector (y) && ismatrix (z)) - if (rows (z) == length (y) && columns (z) == length (x)) - x = x(:)'; - y = y(:); + if (isvector (x) && isvector (y) && ismatrix (z)) + if (rows (z) == length (y) && columns (z) == length (x)) + x = x(:)'; + y = y(:); + else + error ("surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)"); + endif + elseif (ismatrix (x) && ismatrix (y) && ismatrix (z)) + if (! size_equal (x, y, z)) + error ("surface: X, Y, and Z must have the same dimensions"); + endif else - error ("surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)"); - endif - elseif (ismatrix (x) && ismatrix (y) && ismatrix (z)) - if (! size_equal (x, y, z)) - error ("surface: X, Y, and Z must have the same dimensions"); + error ("surface: X and Y must be vectors and Z must be a matrix"); endif - else - error ("surface: X and Y must be vectors and Z must be a matrix"); - endif - elseif (firststring == 3) - z = varargin{1}; - c = varargin{2}; - if (iscomplex (z) || iscomplex (c)) - error ("mesh: X, C arguments must be real"); - endif + case 3 + z = varargin{1}; + c = varargin{2}; + + if (iscomplex (z) || iscomplex (c)) + error ("mesh: X, C arguments must be real"); + endif - if (ismatrix (z) && !isvector (z) && !isscalar (z)) - [nr, nc] = size (z); - x = 1:nc; - y = (1:nr)'; - else - error ("surface: Z argument must be a matrix"); - endif - elseif (firststring == 2) - z = varargin{1}; - c = z; + if (ismatrix (z) && !isvector (z) && !isscalar (z)) + [nr, nc] = size (z); + x = 1:nc; + y = (1:nr)'; + else + error ("surface: Z argument must be a matrix"); + endif - if (iscomplex (z)) - error ("mesh: Z argument must be real"); - endif + case 2 + z = varargin{1}; + c = z; + + if (iscomplex (z)) + error ("mesh: Z argument must be real"); + endif - if (ismatrix (z) && !isvector (z) && !isscalar (z)) - [nr, nc] = size (z); - x = 1:nc; - y = (1:nr)'; - else - error ("surface: Z argument must be a matrix"); - endif - elseif (firststring == 1) - x = 1:3; - y = x'; - c = z = eye (3); - else - bad_usage = true; - return; - endif + if (ismatrix (z) && !isvector (z) && !isscalar (z)) + [nr, nc] = size (z); + x = 1:nc; + y = (1:nr)'; + else + error ("surface: Z argument must be a matrix"); + endif + + case 1 + x = 1:3; + y = x'; + c = z = eye (3); + + otherwise + bad_usage = true; + return; + + endswitch if (firststring < nargin) other_args = varargin(firststring:end);