
How to use fill3 to make a box
7 views (last 30 days)
Show older comments
X= [1 26 26 1]
Y= [0.5 .5 25.5 25.5]
Z= [0 0 0 0]
fill3 (X, Y, Z, 'blue')
X1= [1 26 26 1]
Y1= [0.5 .5 25.5 25.5]
Z1= [25 25 25 25]
fill3 (X1, Y1, Z1, 'red')
I wanna make a box with fill3 but when i typed in the 2nd fill3 code (the red one), it does not produce a surface. How can i make multiple surfaces with fill3
0 Comments
Answers (1)
Darshak
on 6 Mar 2025
The issue with the second surface not being produced using the “fill3” function arises from the vertices not being correctly defined for a 3D face. To achieve the desired box, it is essential to use the “hold on” and “hold off” commands, these commands are used to plot the surfaces on the same axis. Below is the revised code to accomplish this objective:
X = [1 26 26 1];
Y = [0.5 0.5 25.5 25.5];
Z_bottom = [0 0 0 0];
Z_top = [25 25 25 25];
fill3(X, Y, Z_bottom, 'blue');
hold on;
fill3(X, Y, Z_top, 'red');
X_sides = [1 1; 26 26; 26 26; 1 1];
Y_sides = [0.5 0.5; 0.5 0.5; 25.5 25.5; 25.5 25.5];
Z_sides = [0 25; 0 25; 0 25; 0 25];
for i = 1:4
fill3(X_sides(i, :), Y_sides(i, :), Z_sides(i, :), 'green');
end
axis equal;
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
grid on;
hold off;
This is the output we get using the given code.

The “fill3” function can be explored on this documentation page - https://www.mathworks.com/help/releases/R2021a/matlab/ref/fill3.html
The “hold” command can be explored on this documentation page - https://www.mathworks.com/help/releases/R2021a/matlab/ref/hold.html
1 Comment
Voss
on 29 Mar 2025
Note that the green patches (not surfaces, FYI) don't show up on the plot. This is because their coordinates describe the four lateral edges instead of the four lateral faces, i.e., they each have two points instead of four.
See Also
Categories
Find more on Guidance, Navigation, and Control (GNC) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!