Plot a 3D matrix for a three-dimensional object

481 次查看(过去 30 天)
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?

回答(5 个)

Chad Greene
Chad Greene 2017-2-11
编辑:Chad Greene 2017-2-11
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 个评论
Yuzhen Lu
Yuzhen Lu 2017-2-11
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 2017-2-12
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.

请先登录,再进行评论。


Swati Jain
Swati Jain 2020-3-27
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 2019-7-26
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 2020-9-2
编辑:DGM 2023-3-22
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 个评论
DGM
DGM 2023-3-22
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?

请先登录,再进行评论。


wang zhiming
wang zhiming 2023-3-22
编辑:DGM 2023-3-22
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

类别

Help CenterFile Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by