Hannah
Hannah

Reputation: 137

Matlab: Error obtained with rectangle function

I am using MATLAB and want to use the rectangle function in order to plot a rectangle. I want the user to input the coordinates of each corner. I wrote the following:

xR1 = input('x coordinate of the first rectangle corner?');
yR1 = input('y coordinate of the first rectangle corner?');

xR2 = input('x coordinate of the second rectangle corner?');
yR2 = input('y coordinate of the second rectangle corner?');

xR3 = input('x coordinate of the third rectangle corner?');
yR3 = input('y coordinate of the third rectangle corner?');

xR4 = input('x coordinate of the fourth rectangle corner?');
yR4 = input('y coordinate of the fourth rectangle corner?');

XRcoordinates = [xR1 xR2 xR3 xR4]
YRcoordinates = [yR1 yR2 yR3 yR4]

width = max(XRcoordinates) - min(XRcoordinates)
height = max(YRcoordinates) - min(YRcoordinates)

rectangle('Position', min(XRcoordinates), min(YRcoordinates),width,height)
axis([0   max(XRcoordinates)   0   max(YRcoordinates)  ])

When I run it I enter the following

xR1 = 2
yR1 = 3

xR2 = 2
yR2 = 5

xR3 = 4
yR3 = 5

xR4 = 4
yR4 = 3

Yet I obtain the following error message:

Error using rectangle

Can't specify convenience arg for this object

Error in script1 (line 37)

rectangle('Position', min(XRcoordinates),min(YRcoordinates),width,height)

What does the first error message mean? What is wrong?

Upvotes: 1

Views: 1696

Answers (1)

rayryeng
rayryeng

Reputation: 104514

You aren't calling rectangle properly. If you're using the Position flag, it requires a four element vector for the second parameter. You are trying to call rectangle with five parameters. However, the way you are supposed to format this vector exactly corresponds to the input parameters after the Position flag, so all you really need to do is encapsulate those into a vector.

Also, you may want to change the colour of the rectangle to something else, as the default colour is black. Try changing it to something like red. We can append additional parameters after the Position flag.

As such, do this:

rectangle('Position', [min(XRcoordinates), min(YRcoordinates),width,height], 'EdgeColor', 'red');

Upvotes: 3

Related Questions