plotting x y cartesian coordinates
조회 수: 82 (최근 30일)
이전 댓글 표시
Hello,
I have and issue with poltting a map of points on cartesian plane by using plot function.
I have two sets of data x which is a vector of x coordinates in cartesian plane and y which is a vector of y coordinates in cartesian plane.
f.e
x = [0.00134476655097517 0.00134476655097517 0.00134476655097517
0.0013073145485903 0.00127645748949186 0.00124281266824533
0.00124281266824533 0.00121842112126059 0.00121842112126059
0.00121842112126059 0.00121842112126059 0.00121441160849494]
y = [0.343050934411873 0.343050934411873 0.343050934411873
0.333496901108166 0.325625242678495 0.317042424078219
0.317042424078219 0.310820122535397 0.310820122535397
0.310820122535397 0.310820122535397 0.309797292885303]
plot(x,y)
when I'm trying to plot the values are automatically segregated in rising order which in result generates linear function like graph.
The coordinates are x and y are calculated based on a proximity sensor data which is mounted on a stepper motor and scans the enviroment around the center of rotation point returning the distance to the nearest obstacle. Basing on the distance data and the angle which the motor is rotating witch each measurement we can calculate the coordiates in cartesian plane:
I should be able to plot an enviroment map but instead I'm getting a straight line. Where is the problem?
댓글 수: 0
답변 (2개)
DGM
2022년 1월 20일
This isn't the core of the problem, but the way you wrote the x and y vectors causes them to be stored as 4x3 matrices. When you feed them to plot(), they will be plotted as one series per column.
x = [0.00134476655097517 0.00134476655097517 0.00134476655097517 % implicit ;
0.0013073145485903 0.00127645748949186 0.00124281266824533
0.00124281266824533 0.00121842112126059 0.00121842112126059
0.00121842112126059 0.00121842112126059 0.00121441160849494];
y = [0.343050934411873 0.343050934411873 0.343050934411873
0.333496901108166 0.325625242678495 0.317042424078219
0.317042424078219 0.310820122535397 0.310820122535397
0.310820122535397 0.310820122535397 0.309797292885303];
plot(x,y)
legend
If you want them as vectors, you have to continue the lines like so:
x = [0.00134476655097517 0.00134476655097517 0.00134476655097517 ... % continue the same row
0.0013073145485903 0.00127645748949186 0.00124281266824533 ...
0.00124281266824533 0.00121842112126059 0.00121842112126059 ...
0.00121842112126059 0.00121842112126059 0.00121441160849494]
y = [0.343050934411873 0.343050934411873 0.343050934411873 ...
0.333496901108166 0.325625242678495 0.317042424078219 ...
0.317042424078219 0.310820122535397 0.310820122535397 ...
0.310820122535397 0.310820122535397 0.309797292885303]
That said, even with that change they will still be plotted as a straight line because y and x are perfectly proportional. It is a straight line.
y./x
Your problem lies somewhere either in how the angle/distance data is being captured, or an unintended mistake in how it's being converted. The fact that both y and x are monotonically decreasing might be a clue.
댓글 수: 0
Image Analyst
2022년 1월 20일
Those are matrices, not vectors. To turn into a vector and plot, you can use (:) and sort():
x = [0.00134476655097517 0.00134476655097517 0.00134476655097517
0.0013073145485903 0.00127645748949186 0.00124281266824533
0.00124281266824533 0.00121842112126059 0.00121842112126059
0.00121842112126059 0.00121842112126059 0.00121441160849494]
y = [0.343050934411873 0.343050934411873 0.343050934411873
0.333496901108166 0.325625242678495 0.317042424078219
0.317042424078219 0.310820122535397 0.310820122535397
0.310820122535397 0.310820122535397 0.309797292885303]
% Turn x from a 2-D matrix into a sorted vector.
[x, sortOrder] = sort(x(:), 'ascend')
% Make y a vector and sort y the same way so we keep points together.
y = y(:);
y = y(sortOrder);
% Now plot the vectors. y vs. x.
plot(x,y, 'b.-', 'LineWIdth', 2, 'MarkerSize', 30);
grid on;
xlabel('x');
ylabel('y');
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!