To draw a rectangle in MATLAB, you can use the rectangle
function. This function creates a filled or unfilled rectangle, depending on the specified parameters. Below is the general syntax:
main.m38 chars2 lines
The positionVector
variable specifies the position and size of the rectangle. It is a four-element vector that contains the [x,y] coordinates of the bottom-left corner of the rectangle and its width and height.
Here is an example code that draws an unfilled rectangle:
main.m54 chars4 lines
In this example, the rectangle is drawn on a new figure, with its bottom-left corner starting at (0,0) and its sides having lengths of 5 and 3 units, respectively. The axis equal
command ensures that the x and y scales are identical, so that the rectangle appears as a true rectangle.
To draw a filled rectangle, you can add a 'FaceColor'
property to the rectangle
function call. For instance, the following code creates a blue-filled rectangle:
main.m72 chars4 lines
By default, the rectangle function draws the rectangle in the current axes. However, you can specify a different axes as the first input argument to the function, as in rectangle(ax, 'Position', positionVector)
.
gistlibby LogSnag