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?

Answers (4)

Chad Greene
Chad Greene on 11 Feb 2017
Edited: Chad Greene on 11 Feb 2017
Hi Yuzhen,
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));
shading flat
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.
Chad Greene
Chad Greene on 12 Feb 2017
Are you sure there's no regularity to the x and y spacing? It's hard to tell in your plot, but it looks like there might be some common intervals. It's fine is some of them aren't integers, you can still use xyz2grid. I try to avoid any kind of grid fitting if the data are already gridded but inconveniently formatted.

Sign in to comment.

Arjun Tandon
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
shading flat
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
Swati Jain on 27 Mar 2020
For future if someone come across same issue:
One can use pchow
and scatter3
You can see shading with scatter3 by using the follwoing:
scatter3(x, y, z,[],z, '.')

Nilar Htun
Nilar Htun on 2 Sep 2020
x = A(:,:,1);
y = A(:,:,2);
z = A(:,:,3);
[x, y, z] = meshgrid(A(:,1), A(:,2), A(:,3));
shading flag


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!