Hello
I am trying to rotate GPS data, that has been con verted into X & Y coordintes to dsplay lengthwy along the Y axis strting at 0,0
I'm trying to rotate data in a 6135 x 15 table, where the data is layed out as follows 6135 data points of coordintes seperted into X & yXcolumns for each person.
Time I Person 1 x I Person 1 y I Person 2 x I Person 2 y I Person 3 x I Person 3 y .....etc
I would like to rotate this date to run length way along the Y axis. Each line represents data for a different person (7 in total):
Is there a way to rotate a large table of X & Y coordinates.
Thank you in advance for your help!

 採用された回答

Star Strider
Star Strider 2022 年 9 月 20 日

1 投票

See if the rotate function will work.

9 件のコメント

Tania
Tania 2022 年 9 月 20 日
Hi
I get the error:
"H must contain axes children only"
I dont have a third vector (only X & Y, not Z).
To see if the rotation function worked I used one set of X & Y Data (6135 x 2 double) X in the first colum and Y in the second, labled as 'xy1'
I inputted the code below:
direction = [0 1 0]; % rotating around y axis
rotate(xy1,direction,25) % I just inputted 25 as a random degree value
Thanks for your help.
Star Strider
Star Strider 2022 年 9 月 20 日
Try something like this —
x = 0:10;
y = rand(5,11);
figure
hold on
for k = 1:size(y,1)
plot(x, y(k,:))
end
grid
hold off
title('Original')
figure
hold on
for k = 1:size(y,1)
h(k) = plot(x, y(k,:));
end
hold off
grid
rotate(h, [1 0 0], 45)
title('Rotate 45° On ‘X’ Axis')
figure
hold on
for k = 1:size(y,1)
h(k) = plot(x, y(k,:));
end
hold off
grid
rotate(h, [0 1 0], 45)
title('Rotate 45° On ‘Y’ Axis')
figure
hold on
for k = 1:size(y,1)
h(k) = plot(x, y(k,:));
end
hold off
grid
rotate(h, [0 0 1], 45)
title('Rotate 45° On ‘Z’ Axis')
I am not certain how you are plotting your data. I changed the angle to 45° for illustration purposes.
The rotate function will work with plotted line data, however it may be necessary to experiment with it to get the result you want.
.
Tania
Tania 2022 年 9 月 21 日
Hey,
Thanks for this, it’s still not working. I'm receiving an error message, indices on the left side are not compatible with the right side.
I have graphed an X & Y Scatter plot displaying 6135 individual X & Y data points for 7 people (each line representing a different person).
I originally converted the ray Lat/Lon co-ordinates to X & Y coordinates using the following:
[x1,y1]= wgs2utm((data1(:,2)),(data1(:,3))) % I repeated this for each person
I then plotted each converted X & Y data point onto the graph above using the following code:
PlotGPS = scatter(Newdata(:,1),Newdata(:,2)); %Plots all the GPS data in a scatter to select 4 grid coordinates%
hold on
scatter(Newdata(:,3),Newdata(:,4));
scatter(Newdata(:,5),Newdata(:,6));
scatter(Newdata(:,7),Newdata(:,8));
scatter(Newdata(:,9),Newdata(:,10));
scatter(Newdata(:,11),Newdata(:,12));
scatter(Newdata(:,13),Newdata(:,14));
hold off
From here I used:
[PlotGPS] = ginput(4)
To create a boundary around the data points representing an (almost rectangular) field (shown by small light blue circles).
I then used the below code to rotate the boundary
Boundary = PlotGPS([2 4 3 1 2],:);
Boundlong = PlotGPS(4,:) - PlotGPS(2,:); % Length of line - long line
Boundshort = PlotGPS(1,:)- PlotGPS(2,:); % Width of line - short line
%% Rotational Matrix
theta = atan2(Boundlong(2),Boundlong(1)); % theta is in radians
R = [cos(theta) -sin(theta); sin(theta) cos(theta)]; % rotation matrix
BoundR = Boundary*R; %Rotate the field
This worked, however when I tried to apply the same code to the data, because of the large volume of X & Y data points, it did not work.
Is there a way to use the boundary to assist with rotating the X & Y data?
Thanks for your help
Star Strider
Star Strider 2022 年 9 月 21 日
I recommended that you use the rotate function (that I linked to earlier).
My version of that code would be:
figure
hold on
for k = 1:5
k1 = k*2+1;
k2 = k1+1;
h(k) = scatter(Newdata(:,k1), Newdata(:,k2))
end
hold off
the the rotate call would be:
rotate(h, [0 0 1], 45)
or whatever axis and degree you want to rotate the plot. See the documentation I linked to for more on the rotate function, and my earlier Comment for illustrations of how to use rotate effectively to get the result you want.
.
Tania
Tania 2022 年 9 月 21 日
Thanks, but its not quite what I'm after.
I finally got the code above working (had to change my graph from a scatter to plot).
When I select different angles and axis of rotation, it changes the data too much (which is something I don't want), and dosen't ever end up running length-wise across the y axis, despite changing the axis of rottion and inputting different rotation angles.
So while this code works, its not really what I'm after. I think I need to use the X & Y reference points selected from my boundary to somehow rotate the data so it still looks the same, but obviously rotated.
Thanks
Star Strider
Star Strider 2022 年 9 月 21 日
My pleasure!
I have no idea what you want. See the rotate documentation I linked to for more options, since it may be able to do more than what I asked it to do.
Star Strider
Star Strider 2022 年 9 月 23 日
Using your posted data —
LD = open(websave('XY Data(3165,2)','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1131570/XY%20Data%20(6135,2).mat'));
XY_P1 = LD.XY_P1;
x = XY_P1(:,1);
y = XY_P1(:,2);
B = [x ones(size(x))] \ y;
slope = B(1) % Line Slope
slope = 1.3159
angd = atand(slope) % Arctangent Of Slope In Degrees
angd = 52.7676
ang2d = atan2d(mean(diff(y)), mean(diff(x))) % Arctangent Option Using 'atan2d'
ang2d = 55.1069
figure
p(1) = plot(x, y);
grid
axis('equal')
title('Original')
figure
p(1) = plot(x, y);
grid
axis('equal')
rotate(p(1), [0 0 1], -angd) % Rotate Line Using Negative Arctangent Of Calculated Slope
title('Rotated To Parallel X-Axis')
So the rotate function appears to work here.
This is the best I can do with these data. Fortunately, the data here appear to be in a straight line, so this appears to be obvious. If they are not in a relatively straight line, the code should still work, although it might not be as obvious, since it will use the linear regression slope value to do the rotation.
Using atan2d to calculate the slope angle could be more robust (I included an option using it), however if the angles are restricted to the first two quadrants, atand should work well enough.
NOTE — The rotation is with respect to the Z axis. This is not intuitive (at least to me), however it produces the desired result.
.
Tania
Tania 2022 年 9 月 27 日
Thanks for your help with this!
Star Strider
Star Strider 2022 年 9 月 27 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2022 年 9 月 20 日

0 投票

p1 = rand(2,1) ;
p2 = rand(2,1) ;
%%rotation matrix
th = 90*pi/180 ;
R = [cos(th) -sin(th) ;sin(th) cos(th)] ;
%%rotate points
pr2 = R*p2 ;
figure
hold on
plot([p1(1) p2(1)],[p1(2) p2(2)] ,'r') ;
plot([p1(1) pr2(1)],[p1(2) pr2(2)] ,'b') ;

5 件のコメント

Tania
Tania 2022 年 9 月 20 日
Hi
Thanks, because my table has a significantly larger number of rows, when I try to rotate the data using the code:
pr2 = R*p2 ;
I receive an error "Incorrect dimensions for matrix multiplication".
I then add the. as below:
pr2 = R.*p2 ;
However, I receive another error stating "Arrays have incompatible sizes for this operation."
Thanks for your help
KSSV
KSSV 2022 年 9 月 21 日
Attach your data. MAke your data a m*2 array and then multiply.
Tania
Tania 2022 年 9 月 21 日
Hello
I have attached part of my data. This shows X & Y data for one person. Column one is X data and column 2 in Y data. The rows are individual X & Y data points samples across time.
Thanks
Tania
Tania 2022 年 9 月 23 日
Hello
I've attempted to rotate my data using the below code:
Note xy2 is the data set (6135x2 matrix)
theta = atan2(xy2(:,2),xy2(:,1));
R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
RR = R(1:6135,:).*xy2;
As R is double the length of the xy2 data set I select the first 6135 rows in enable multiplication for the rotated (RR) dataset.
The below graph is the original xy2 dataset in a scatter:
This next graph below is of the rotated data RR
Instead of rotating it has flipped the data set. I need the data rotted so it runs longitudinally along the Y axis.
Without guessing the theta angle is there a way to correctly determine the rotation angle I need?
Thanks is advance for your help.
Tania
Tania 2022 年 9 月 23 日
As a reference (continued from comment above):
I originally converted the ray Lat/Lon co-ordinates to X & Y coordinates using the following:
[x1,y1]= wgs2utm((data1(:,2)),(data1(:,3))) % I repeated this for each person
I then plotted each converted X & Y data point onto the graph above using the following code:
PlotGPS = scatter(Newdata(:,1),Newdata(:,2)); %Plots all the GPS data in a scatter to select 4 grid coordinates%
hold on
scatter(Newdata(:,3),Newdata(:,4));
scatter(Newdata(:,5),Newdata(:,6));
scatter(Newdata(:,7),Newdata(:,8));
scatter(Newdata(:,9),Newdata(:,10));
scatter(Newdata(:,11),Newdata(:,12));
scatter(Newdata(:,13),Newdata(:,14));
hold off
From here I used:
[PlotGPS] = ginput(4)
To create a boundary around the data points representing an (almost rectangular) field (shown by small light blue circles).
I then used the below code to rotate the boundary
Boundary = PlotGPS([2 4 3 1 2],:);
Boundlong = PlotGPS(4,:) - PlotGPS(2,:); % Length of line - long line
Boundshort = PlotGPS(1,:)- PlotGPS(2,:); % Width of line - short line
%% Rotational Matrix
theta = atan2(Boundlong(2),Boundlong(1)); % theta is in radians
R = [cos(theta) -sin(theta); sin(theta) cos(theta)]; % rotation matrix
BoundR = Boundary*R; %Rotate the field
This worked, (see above).
From my last post I'm now able to rotate, the X & Y dta points, hopwever as you can see in my last post its not rotating correctly.
Is there a way to use the boundary reference information to rotate my X & Y data?
Thanks in advance for your help/insight.

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2022 年 9 月 20 日

コメント済み:

2022 年 9 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by