Plot a 3D matrix for a three-dimensional object
823 views (last 30 days)
I have obtained a 3D matrix A(:,:,3) for the geometry of an object. A(:,:,1) is a matrix corresponding to x coordinates in the world coordinate system, and A(:,:,2) stores y coordinates and A(:,:,3) stores the z coordinates. So each point the object has three coordinates (x,y,z). It is noted that these coordinates are scattered, and some of them are non-integer.
Now I am going to plot the 3D surface or shape of the object. I am wondering what Matlab function or functions are best or suitable for the purpose.
A possible way is to use plot3 as follows:
x = A(:,:,1);
y = A(:,:,2);
z = A(:,:,3);
figure; plot3(x,y,z,'.-'); %the figure is attached below
But this function does not give a desired figure. less authentic, no shading or camlight functions supported. trisurf.m seems a good choice for the problem. Are there any other options?
Chad Greene on 11 Feb 2017
Edited: Chad Greene on 11 Feb 2017
Are the x and y values evenly spaced, or are they randomly scattered about? If the x and y values are evenly spaced, you can use xyz2grid.
[X,Y,Z] = xyz2grd(A(:,1),A(:,2),A(:,3));
But if the data points are scattered, you'll have to grid them with one of the scattered interpolation functions. My go-to gridding function is John D'Errico's gridfit, which is on File Exchange.
Arjun Tandon on 26 Jul 2019
I don't have the xyz2grd function, so I've done it this way. It's not pretty, but it's worked for me.
%% Cleaning up
%% Generating 3D data
for i = 1:100
for j = 1:100
for m = 1:100
A(i,j,m) = i+j+m;
%% This is the important part. Here, I'm just extracting the
%% vector data and storing it in variables.
X = A(:,1);
Y = A(:,2);
Z = A(:,3);
T = [X,Y,Z]; %Creating a matrix (10 by 10 by 10)
%% Graphical bits
me. Hypothetically, you could also modify Chad's method (shown below); I was too lazy to try.
T = [A(:,1), A(:,2), A(:,3)];
Swati Jain on 27 Mar 2020
For future if someone come across same issue:
One can use pchow
You can see shading with scatter3 by using the follwoing:
scatter3(x, y, z,,z, '.')