Plot Cubes in Matalb

7 views (last 30 days)
Chad
Chad on 3 Jan 2012
Answered: Raj Gopal Mishra on 25 Jul 2020
Hi...I have a question about plotting cubes in Matlab. I have a program that generates n-cubes where each cube has a value associated with it. For example, I start with a simple 3x3x3 center that represents the center of cube and I have a value for each cube. So I have
CubeCenter = 3x27: Where column 1 is x, column 2 is y, and column 3 is the z coordinates.
CubeCenterValue = 1x27: Where is I have the value for each cube center.
Is there a way to plot the 27 cubes where it has a different color. Furthermore, is there a way to plot the cubes in 3-d and also plot a cross-section of just 9 of the cubes in 2-d with a different color.
Thanks in advance...Chad

Answers (1)

Raj Gopal Mishra
Raj Gopal Mishra on 25 Jul 2020
Cube Plot
X=linspace(-1,1,101); % Define point on x axis
Y=ones(size(X)); % define points of y axis
Z=Y;% define point on z axis
%%SURFACE AT Z=1
plot3([X fliplr(X)], [Y (-1*Y)], [Z (Z)],'r')
xlabel('X')
hold on
%%SURFACE AT Z=-1
plot3([X fliplr(X)],[Y (-1*Y)],[(-1*Z) (-1*Z)],'b')
xlabel('X')
hold on
%%SURFACE AT Y=1
plot3([X fliplr(X)], [Y Y], [Z (-1*Z)], 'k')
ylabel('Y')
hold on
%%SURFACE AT Y=-1
plot3([X fliplr(X)],[(-1*Y) (-1*Y)], [Y (-1*Y)], 'y')
ylabel('Y')
hold on
%%SURFACE AT X=1
plot3([Z Z], [Y (-1*Y)],[X fliplr(X)], 'm')
zlabel('Z')
hold on
%%SURFACE AT X=-1
plot3([(-1*Z) (-1*Z)], [Y (-1*Y)],[X fliplr(X)], 'c')
zlabel('Z')
hold off
grid
Cube Patch
X=linspace(-1,1,101); % Define point on x axis
Y=ones(size(X)); % define points of y axis
Z=Y;% define point on z axis
figure
%%SURFACE AT Z=1
patch([X fliplr(X)], [Y (-1*Y)], [Z (Z)],'r')
xlabel('X')
hold on
%%SURFACE AT Z=-1
patch([X fliplr(X)],[Y (-1*Y)],[(-1*Z) (-1*Z)],'b')
xlabel('X')
hold on
%%SURFACE AT Y=1
patch([X fliplr(X)], [Y Y], [Z (-1*Z)], 'k')
ylabel('Y')
hold on
%%SURFACE AT Y=-1
patch([X fliplr(X)],[(-1*Y) (-1*Y)], [Y (-1*Y)], 'y')
ylabel('Y')
hold on
%%SURFACE AT X=1
patch([Z Z], [Y (-1*Y)],[X fliplr(X)], 'm')
zlabel('Z')
hold on
%%SURFACE AT X=-1
patch([(-1*Z) (-1*Z)], [Y (-1*Y)],[X fliplr(X)], 'c')
zlabel('Z')
hold off
grid
view(40,40)

Community Treasure Hunt

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

Start Hunting!