How to Graph a Cube in MATLAB

Written by sean hill
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Graph a Cube in MATLAB
Learn how to plot a 3D cube in Matlab. ( Images)

Matlab is a programming language that allows users to enter, manipulate plot, and analyse data in a variety of ways. Matlab also includes a graphing utility, enabling users to graph different shapes. To graph a cube in Matlab, there are a few options. One approach is to use one of Matlab's built-in functions called "patch." This function requires a few lines of code and outputs a 3D multicolour cube.

Skill level:
Moderately Easy

Things you need

  • Matlab R2006 or later version

Show MoreHide


  1. 1

    Open Matlab.

  2. 2

    Click "File" and "New." Click "m-file."

  3. 3

    Type "patch([0 0 1 1],[0 1 1 0],[1 1 1 1],'r')" --minus the quotation marks -- on the first line of the new m-file.

  4. 4

    Type "patch([0 1 1 0],[0 0 0 0],[0 0 1 1],'b')" on the next line.

  5. 5

    Type "patch([0 0 0 0],[0 1 1 0],[0 0 1 1],'y')" on the next line.

  6. 6

    Type "view(-37.5, 30)" on the fourth line.

  7. 7

    Type "axis square" on the last line.

  8. 8

    Click the "Run" button with the green arrow to execute the m-file. A 3D multicolour cube should appear.

Tips and warnings

  • Another option for graphing a cube is to download a free add-on such as Matgraph Toolbox. This toolbox is available from the Matlab website for free. When you download Matgrah, place the file anywhere you want on your computer. To make Matlab recognise the toolbox, simply type "addpath('filepath')" in the command window. For example: addpath(c:/documents/matlab/myfiles). To graph a cube, type "cube(g,k)" in the command window, where "g" and "k" are the desired dimensions.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.