# HG changeset patch # User Rik # Date 1329417427 28800 # Node ID 5736d93b22d059d9194d5da95d2d2a2f4061ac04 # Parent ba01a38bc5c1ee88b0b9197632364ec4a99e3d0a griddata.m: Accept vectors in any orientation (Bug #33539) * griddata.m: Accept vectors in any orientation (Bug #33539) diff -r ba01a38bc5c1 -r 5736d93b22d0 scripts/geometry/griddata.m --- a/scripts/geometry/griddata.m Wed Feb 15 18:41:12 2012 -0500 +++ b/scripts/geometry/griddata.m Thu Feb 16 10:37:07 2012 -0800 @@ -38,11 +38,8 @@ ## xi and yi are not "meshgridded" if both are vectors ## of the same size (for compatibility) -function [rx, ry, rz] = griddata (x, y, z, xi, yi, method) +function [rx, ry, rz] = griddata (x, y, z, xi, yi, method = "linear") - if (nargin == 5) - method = "linear"; - endif if (nargin < 5 || nargin > 7) print_usage (); endif @@ -51,27 +48,32 @@ method = tolower (method); endif + ## Meshgrid if x and y are vectors but z is matrix if (isvector (x) && isvector (y) && all ([numel(y), numel(x)] == size (z))) [x, y] = meshgrid (x, y); - elseif (! all (size (x) == size (y) & size (x) == size (z))) - if (isvector (z)) - error ("griddata: X, Y, and Z, be vectors of same length"); - else - error ("griddata: lengths of X, Y must match the columns and rows of Z"); + endif + + if (isvector (x) && isvector (y) && isvector (z)) + if (! isequal (length (x), length (y), length (z))) + error ("griddata: X, Y, and Z must be vectors of the same length"); endif + elseif (! size_equal (x, y, z)) + error ("griddata: lengths of X, Y must match the columns and rows of Z"); endif ## Meshgrid xi and yi if they are a row and column vector. if (rows (xi) == 1 && columns (yi) == 1) [xi, yi] = meshgrid (xi, yi); + elseif (isvector (xi) && isvector (yi)) + ## Otherwise, convert to column vectors + xi = xi(:); + yi = yi(:); endif if (! size_equal (xi, yi)) error ("griddata: XI and YI must be vectors or matrices of same size"); endif - [nr, nc] = size (xi); - x = x(:); y = y(:); z = z(:); @@ -138,6 +140,7 @@ elseif (nargout == 0) mesh (xi, yi, zi); endif + endfunction @@ -183,3 +186,18 @@ %! zz2(isnan (zz)) = NaN; %! assert (zz, zz2, 100*eps); +%% Test input validation +%!error griddata () +%!error griddata (1) +%!error griddata (1,2) +%!error griddata (1,2,3) +%!error griddata (1,2,3,4) +%!error griddata (1,2,3,4,5,6,7) +%!error griddata (1:3, 1:3, 1:4, 1:3, 1:3) +%!error griddata (1:3, 1:4, 1:3, 1:3, 1:3) +%!error griddata (1:4, 1:3, 1:3, 1:3, 1:3) +%!error griddata (1:4, 1:3, ones (4,4), 1:3, 1:3) +%!error griddata (1:4, 1:3, ones (3,5), 1:3, 1:3) +%!error griddata (1:3, 1:3, 1:3, 1:4, 1:3) +%!error griddata (1:3, 1:3, 1:3, 1:3, 1:4) +