image thumbnail
from xyzplotter by Jack Kohoutek
Transforms lists xin,yin,zin into matrices which can be used with the functions surf and mesh.

xyzplotter(xin,yin,zin)
% [X Y Z] = XYZPLOTTER(XIN,YIN,ZIN) transforms lists XIN, YIN, ZIN which
% are cartesian triples into matrixes X,Y,Z which can be used with the functions 
% SURF,MESH, etc.  Missing values will be filled with NaNs.
%
% Repeat XIN and YIN values are fine, they are not repeatedly added to the output matrices. 
% The cell size of the resulting grid is therefore the number of unique XIN
% values cross the number of unique YIN values.
%
% If you  would like to interpolate the NaNs, I suggest inpaint_nans, file exchange number 4551,
% here is the link:
% http://www.mathworks.com/matlabcentral/fileexchange/4551
% Jack Kohoutek 2009 jack.kohoutek@gmail.com


function [x y z] = xyzplotter(xin,yin,zin)
tic
if nargin~=3
    error('Incorrect number of inputs')
end
if length(xin)~=length(yin)||length(xin)~=length(zin)||length(yin)~=length(zin)
    error('Length of arguments must be equal');
end
if size(xin,2)~=1||size(yin,2)~=1||size(zin,2)~=1
    error('Arrays must be n x 1');
end

xu=unique(xin);
yu=unique(yin);
zu=unique(zin);

[nx binx]=histc(xin,xu);
[ny biny]=histc(yin,yu);
[nz binz]=histc(zin,zu);

[x y]=meshgrid(xu,yu);
z=accumarray([binx,biny],zin,[size(x,2) size(y,1)],@sum,NaN);
z=z';
toc

Contact us at files@mathworks.com