Plot a 3D matrix for a three-dimensional object

481 views (last 30 days)
Yuzhen Lu
Yuzhen Lu on 11 Feb 2017
Commented: DGM on 22 Mar 2023
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 (5)

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));
surf(X,Y,Z)
shading flat
camlight
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.
  2 Comments
Yuzhen Lu
Yuzhen Lu on 11 Feb 2017
Thanks for your answer!
These coordinates are not evenly spaced, and some of them are non-integer. I will try your suggestion
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.


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, '.')

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
clc
clear
close
%% Generating 3D data
for i = 1:100
for j = 1:100
for m = 1:100
A(i,j,m) = i+j+m;
end
end
end
%% 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
surf(T)
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)];

Nilar Htun
Nilar Htun on 2 Sep 2020
Edited: DGM on 22 Mar 2023
x = A(:,:,1);
y = A(:,:,2);
z = A(:,:,3);
[x, y, z] = meshgrid(A(:,1), A(:,2), A(:,3));
surface(x,y,z);
figure;
scatter3(x,y,z,'.-');
figure;
shading flag
  1 Comment
DGM
DGM on 22 Mar 2023
This appears to be a nonfunctioning collage of copy-pasted code.
% so A is a 3D array with at least 3 pages
% now x,y,z are 2D arrays corresponding to those pages
x = A(:,:,1);
y = A(:,:,2);
z = A(:,:,3);
% throw all that away and sample the first three
% columns of the first page of A instead.
% now x,y,z are all 3D arrays
[x, y, z] = meshgrid(A(:,1), A(:,2), A(:,3));
% surface(), surf(), scatter3() do not accept 3D arrays
% surface()/surf() work with gridded data
% which wasn't what the original problem had
surface(x,y,z);
figure; % normally you create a figure _before_ you use it
scatter3(x,y,z,'.-');
figure;
% there is no such option for shading
% either this is supposed to be
% shading flat
% or
% colormap(flag)
shading flag
What's the point of posting this?

Sign in to comment.


wang zhiming
wang zhiming on 22 Mar 2023
Edited: DGM on 22 Mar 2023
rng(9,'twister')
data = rand(10,10,10);
data = smooth3(data,'box',5);
patch(isocaps(data,.5),...
'FaceColor','interp','EdgeColor','none');
p1 = patch(isosurface(data,.5),...
'FaceColor','blue','EdgeColor','none');
isonormals(data,p1)
view(3);
axis vis3d tight
camlight left;
colormap jet
lighting gouraud

Tags

Community Treasure Hunt

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

Start Hunting!