New x- and y-data after rotation, using view(-90,90) and set(gca, 'YDir', reverse')

3 views (last 30 days)
Hey,
I'm trying to fit a rectangle into an "original" rectangle. They'll always have the same dimensions, but sometimes the rectangle I'm given, will lie down in contrast to the original. Then I just used view(-90,90) and set(gca, 'YDir', reverse'), since this seems to do the work, but when I use copyobj to copy my rotated rectangle into the figure with the original rectangle, it always print the non-rotated rectangle, so it still doesn't fit the original.
I tried to look at the XData and YData before and after using view and reverse, and they are the same..
So my question is, how do I get the data for my "changed" plot?
A small example: p = plot([1,4],[3,3]) % a horizontal line view(-90,90) set(gca,'YDir','reverse')
% now it's a vertical line, but its data is still the same, and I need the new data which now should be x=[3,3] and y=[1,4].
And is it possible to find the data, of a plot with no handle name? Like if I hadn't call my plot 'p'?

Answers (1)

AJ von Alt
AJ von Alt on 20 Jan 2014
The functions that you are using affect the way the axes are drawn on a figure, not the underlining data. If all you want to do is rotate the data about the origin, you can use a rotation matrix.
Here's a quick example:
% anonymous function dcm2d the points in pt by theta radians
% counterclockwise about origin
dcm2d = @(pt , theta) [ cos(theta) , -sin(theta) ; sin(theta) cos(theta ) ] * pt;
origRect = [-2 , 2 , 2, -2; %x
-1 , -1, 1, 1]; %y
% Rotate the points by pi/2 radians = 90 degrees
rotRect = dcm2d( origRect , pi/2 );
%plot the two rectangles
figure;
plot(origRect(1,:) , origRect(2,:) , rotRect(1,:) , rotRect(2,:) );
As for your second question, it is possible to retrieve the plotted data without the line's handle. The lines on the axes are "children" of the axes object. You can get the handles of these lines by using the get command on the axes object with the property name 'children'.
h = get(gca, 'children')
You can then retrieve the data using get function with the property names 'xdata' and 'ydata'.
data1 = [get( h(1) , 'xdata' ) ; get( h(1) , 'ydata' ) ];
data2 = [get( h(2) , 'xdata' ) ; get( h(2) , 'ydata' ) ];

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!