MATLAB For Dummies, 2nd Edition
Book image
Explore Book Buy On Amazon

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 The following steps help you create an image of your own.

  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.


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

About This Article

This article is from the book:

About the book authors:

Jim Sizemore is professor of physics and engineering at Tyler Junior College. For over 25 years, Sizemore has worked in the semiconductor and software industries as a process engineer, device physicist, and software developer. John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++.

This article can be found in the category: