# How to draw a cube in matlab

Written by joe friedman
• Share
• Tweet
• Share
• Email

MATLAB from Mathworks is a very useful computer software program for entering and analysing data. The software can perform complex mathematical calculations and engineering simulations. You can also use it to graph any shape you can mathematically describe, including two-dimensional circles, squares and polygons as well as three-dimensional spheres, cubes and other cylinders. The simplest way to construct a cube is to use a matrix to define six two-dimensional squares and then define another matrix to tell MATLAB the vertices at which to put them together.

Skill level:
Moderately Easy

## Instructions

1. 1

Define a six row by four column matrix to define the six cube faces by typing the following:

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

2. 2

Define an eight row by three column matrix to define the vertices at which the faces meet by typing the following:

vm = [0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1]

3. 3

Plot the cube by typing the following code, which also gives each face a different colour and creates the cube at a convenient viewing angle:

patch('Vertices',vm,'Faces',fm, 'FaceVertexCData',hsv(6),'FaceColor','flat'); view(-37.5, 30)

### Don't Miss

#### References

• All types
• Articles
• Slideshows
• Videos
##### Sort:
• Most relevant
• Most popular
• Most recent