jbojcic
jbojcic

Reputation: 973

Matlab Rectangle not working after swapping x and y axes

I need to swap x and y axes and draw a rectangle. I do the swap with "view(-90, 90)" and axes are swapped correctly but the rectangle is not drawn. Code:

axis([1 11 1 11])
xlabel('x')
ylabel('y')
rectangle('Position',[5 5 1 1], 'FaceColor','k')
view(-90,90) 
set(gca,'ydir','reverse')
set(gca,'xdir','reverse')
set(gca, 'YAxisLocation', 'right')

When I draw something with "plot" instead of "rectangle", e.g.

X = (1:11);
Y = (1:11);
plot(X, Y);

it is drawn correctly.

Upvotes: 0

Views: 88

Answers (3)

Hoki
Hoki

Reputation: 11802

Well it seems that as soon as you rotate the point of view (even by 1 degree), the rectangle is still in the axes children list but it is not rendered. I tried with all different renderer (painter,openGL, and zbuffer) and it is the same behavior (at least on my system: Matlab 2013a, win8)

So a quick workaround is to use a patch object instead. I tried these and they are rendered regardless of the point of view. The properties are mostly the same, except for the rounded angle but if you don't need that you'll be better off with patch anyway.

If for ease of use you want to mimic the calling syntax of rectangle, you can package your call in a custom function recpatch. And if the coordinates and the color are the only things you want to set you can even use an inline function:

%// define an inline helper function to create a rectangle patch object
recpatch = @(x,y,w,h,c) patch([x x+w x+w x],[y y y+h y+h], c ) ;

%// create your rectangle (and retrieve the handle if you want to set other properties later)
hp = recpatch(5,5,1,1,'k') ;

This rectangle will pass the test of rotation ;-)

Upvotes: 2

bk sumedha
bk sumedha

Reputation: 63

Try this:

rectangle('Position',[1 2 5 6])
axis([1 11 1 11])

Upvotes: 0

Jerry
Jerry

Reputation: 4408

Actually the axes ares swapped, but you're drawing a square so you don't notice that. Try this and you'll notice the swap:

rectangle('Position',[5 5 2 1], 'FaceColor','k')

Upvotes: 0

Related Questions