How can I draw multiple 3d cubes in matlab

3d, cube, matlab

Solution

Maybe you want something like this:

you just need to slightly modify your original code: 1. define a new cube which should be placed inside the first one. 2. please remember add 'hold on' after 'patch'.

clf;
figure(1);
format compact 
h(1) = axes('Position',[0.2 0.2 0.6 0.6]);
vert = [1 1 -1; 
        -1 1 -1; 
        -1 1 1; 
        1 1 1; 
        -1 -1 1;
        1 -1 1; 
        1 -1 -1;
        -1 -1 -1];

fac = [1 2 3 4; 
       4 3 5 6; 
       6 7 8 5; 
       1 2 8 7; 
       6 7 1 4; 
       2 3 5 8];

% I defined a new cube whose length is 1 and centers at the origin.
vert2 = vert * 0.5;  
fac2 = fac;

patch('Faces',fac,'Vertices',vert,'FaceColor','b');  % patch function
axis([-1, 1, -1, 1, -1, 1]);
axis equal;

hold on;

patch('Faces', fac2, 'Vertices', vert2, 'FaceColor', 'r');
material metal;
alpha('color');
alphamap('rampdown');
view(3);

Problem

I'm trying to draw 2 cubes in one M file. This is my code: ``` format compact h(1) = axes('Position',[0.2 0.2 0.6 0.6]); vert = [1 1 1; 1 2 1; 2 2 1; 2 1 1 ; ... 1 1 2;1 2 2; 2 2 2;2 1 2]; fac = [1 2 3 4; ... 2 6 7 3; ... 4 3 7 8; ... 1 5 8 4; ... 1 2 6 5; ... 5 6 7 8]; patch('Faces',fac,'Vertices',vert,'FaceColor','r'); % patch function material shiny; alpha('color'); alphamap('rampdown'); view(30,30); ``` Now, I want to draw second cube and replace inside first one. Does anyone know how I can do that?

Original source