# How to generate and plot a 3D structured mesh in MATLAB?

50 views (last 30 days)
Hi ... I have tried to solve the following problem, but I failed. Can you help me?
Use MATLAB to generate and plot a three-dimensional grid of (6 X 8 X 10) resolution for a (8m X 6m X 5m) rectangular body.
The following is my code and it's not working..!! Help
clear, clc
W=8; % Width
H=6; % Hight
D=5; % Depth
i=6; % Nods number (x-direction)
j=8; % Nods number (y-direction)
k=5; % Nods number (z-direction)
x=linspace(0,W,i); % x
y=linspace(0,H,j); % y
z=linspace(0,D,k); % z
[x,y,z]=meshgrid(x,y,z);
plot3(x,y,z,'ok')

darova on 28 Jan 2020
Try
[X,Y,Z] = meshgrid(x,y,z);
plot3(X(:),Y(:),Z(:),'ok')

Show 1 older comment
darova on 29 Jan 2020
I added NaN at the end of each dimension to break up line
W=8; % Width
H=6; % Hight
D=5; % Depth
j=6; % Nods number (x-direction) (columns)
k=8; % Nods number (y-direction) (3d dimension)
i=5; % Nods number (z-direction) (rows)
x = [linspace(0,W,j) nan]; % x
y = [linspace(0,H,k) nan]; % y
z = [linspace(0,D,i) nan]; % z
[X1,Y1,Z1] = ndgrid(x,y,z);
[Y2,X2,Z2] = ndgrid(y,x,z);
[Z3,X3,Y3] = ndgrid(z,x,y);
plot3(X1(:),Y1(:),Z1(:),'o-k') % X lines
hold on
plot3(X2(:),Y2(:),Z2(:),'-k') % Y lines
plot3(X3(:),Y3(:),Z3(:),'-k') % Z lines
hold off
If you don't mind, can you be my first friend in this community... I'm new to this comunity and this was my first question.
I don't know how to invite you.
darova on 29 Jan 2020
of course!

Cameron B on 28 Jan 2020
I’m not sure what all you wanted to do. Is this what you were looking for?
clear
clc
W=8; % Width
H=6; % Hight
D=5; % Depth
i=6; % Nods number (x-direction)
j=8; % Nods number (y-direction)
k=5; % Nods number (z-direction)
x=linspace(0,W,10); % x
y=linspace(0,H,10); % y
z=linspace(0,D,10); % z
plot3(x,y,z,'ok')
xlabel('x label')
ylabel('y label')
zlabel('z label')