rectangle

Draws a rectangle, rounded rectangle or ellipse in 2-D.

Category: Graphics > Formatting and Annotation

Syntax

rectangle('Position', [x y w h])

Draws a rectangle with lower-left corner (x, y), width w and height h.

rectangle(..., 'Curvature', c)

Rounds the corners. [1 1] gives an ellipse (a circle if w equals h).

rectangle(..., 'FaceColor', color, 'EdgeColor', color)

Sets the fill and the outline.

r = rectangle(___)

Returns the rectangle object.

Description

rectangle adds a shape to the current axes without clearing them, which makes it useful to mark regions of interest or to draw circles around points. A circle appears round only when the axes have equal scaling, so call axis equal first.

The position is in data coordinates, so the shape moves and stretches with the axes limits.

Input arguments

Position
Four-element vector [x y width height] in data units.
Curvature
Number or [cx cy] from 0 (sharp corners) to 1 (fully rounded). Default 0.
FaceColor
Fill color. The default is no fill.

Output arguments

r
Rectangle object, class matlab.graphics.primitive.Rectangle.

Example

A circle and a box

axis equal
xlim([0 6]); ylim([0 4])
r = rectangle('Position', [1 1 2 2], 'Curvature', [1 1]);
rectangle('Position', [3.5 1 2 2], 'FaceColor', [0.8 0.9 1]);
r.Position
r.Curvature
Output
ans = 1×4

     1     1     2     2
ans = 1×2

     1     1

See also

line text fill patch annotation