help with XYZ data file
Show older comments
I am trying to understand a XYZ datafile from a 3D file. Example file.
I was able to google how to open the XYZ file in matlab and it gave me code that works (provided below) but I would like help manipulating the code. First question, I would like to be able to plot some of the planes of XY with fixed Z values/range. Can this be done direclty from the MATLAB variables XYZ used below? This seems to be making lines, it is some kind of vector data?
figure, imagesc(X(:,1),Y(:,1),Z(100:1000,1))
What is the scatterinterpolant doing? How can i get XY planes for each Z ?
clear
data = readmatrix('#1-0331_06_mesh.xyz', 'FileType', 'text');
X = data(:,1);
Y = data(:,2);
Z = data(:,3);
% Create a grid
[xq, yq] = meshgrid(linspace(min(X), max(X), 100), linspace(min(Y), max(Y), 100));
% Interpolate Z values onto the grid. Different interploation methods
F = scatteredInterpolant(X, Y, Z);
zq = F(xq, yq);
% Plot the surface
figure, surf(xq, yq, zq);
shading interp; % Smooths the surface
axis equal
5 Comments
Matt J
on 29 Apr 2026 at 18:43
Please attach the X,Y,Z data in a .mat file so we can demonstrate solutions.
The main problem is that we need to get the data into a 3D image array as we want to overlay it onto a 3D image set.
A 3D image is defined by quadruplets (i,j,k,v) where (i,j,k) are voxel coordinates in the 3D image and v is the voxel value at (i,j,k).
Right now, you don't have quadruplets, but rather triplets (x,y,z). It is not clear what mapping
(x,y,z)--->(i,j,k,v)
you are trying to achieve.
Accepted Answer
More Answers (3)
First question, I would like to be able to plot some of the planes of XY with fixed Z values/range.
The terminology "planes of XY" is not entirely clear to me, but I suspect that you want,
contour(xq,yq,zq)
In the documentation for contour, you will also see that there are further options to specify the precise isocontour z-levels that you want.
2 Comments
JM
on 29 Apr 2026 at 19:30
I'm afraid you're not going to be able to make your meaning understood by using fake Matlab command syntax like imagesc(x,y @z)
Right now you have a surface z(x,y). A surface and an image are not the same thing. If you take the intersection of a surface and a plane, you get a curve, not an image.
If x,y are integer-valued, and are supposed to represent pixel coordinates in some image, and you want to assign a value to each pixel (x,y), you must explain how the pixel value at each x,y is to be computed from (x,y,z).
"What is the scatterinterpolant doing?"
Creating a set of interopolated z values, zq, on a regular grid xq, yq over the range of X, Y (100 points in each direction).
"How can i get XY planes for each Z ?"
nZ=size(data,3); % the number of planes in data array
for i=1:nZ % iterate over all planes
Zi=data(:,:,i); % retrieve the ith plane
% do whatever wanted here...
end
NOTA BENE:
MATLAB has regular arrays but the data in X, Y may not be regularly spaced (hence the use of the scatteredinterpolant() above, probably).. The above retrieves the actual data points only at whatever are the specific locations given.
This might be what you're looking for:
zq=fspecial('gaussian',200,40);
Nlevels=5;
zRanges=linspace(min(zq(:)) ,max(zq(:)), Nlevels+1);
levelMap = discretize(zq,zRanges);
Masks = levelMap==reshape( 1:Nlevels,1,1,[]);
tiledlayout('h');
nexttile; imshow(zq,[]);
nexttile; imshow(levelMap,[]);
nexttile; imshow(Masks(:,:,3).*zq,[]); set(gcf(),'Position',[34 487 1529 463])
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

