How to Create Images in MATLAB

By Jim Sizemore, John Paul Mueller

You can create images using MATLAB. All you need is a mathematical model that describes the points used to describe the image shape. For example, to draw a square, you simply provide the x- and y-axis coordinate for each corner. You can see a number of these shapes demonstrated at MathWorks.com. The following steps help you create an image of your own.

image0.jpg

  1. Type XSource = [1, 1, 5, 5]; and press Enter.

  2. Type YSource = [1, 5, 5, 1]; and press Enter.

    The XSource and YSource variables contain coordinates to draw a square. The lower-left corner is at 1,1; the upper-left corner is at 1,5; the upper-right corner is at 5,5; and the lower-right corner is at 5,1.

  3. Type fill(XSource, YSource, ‘b’); and press Enter.

    MATLAB creates the image, but the image consumes the entire drawing area. Notice that the image is filled with blue. You can choose any color you like using the options below. To see the image set apart from the plot area, you need to change the x and y limits.

  4. Type set(gca, ‘XLim’, [0, 6]); and press Enter.

  5. Type set(gca, ‘YLim’, [0, 6]); and press Enter.

    The image is now clear and in the center of the plot.

    image1.jpg

Selecting a color for your plot is important. This list contains the most common colors — those you can specify using a color letter or descriptive name. However, you can specify partial RGB values. For example, an RGB value of [.5, .25, 0] provides a nice brown. Each entry for red, green, and blue must have a value between 0 and 1.

RGB Value Color Letter Description
[1 1 0] y yellow
[1 0 1] m magenta
[0 1 1] c cyan
[1 0 0] r red
[0 1 0] g green
[0 0 1] b blue
[1 1 1] w white
[0 0 0] k black