Hello! I've got a little question about plotting points. In my project, I'm getting x and y coordinates and plotting them just when I read them from the socket, and I get something like this:
Just after plotting the coordinates I'm indexing them in two different vectors (one for x coordinates and the other for y coordinates). The problem is, when I try to plot them again in another script (or in another figure) the result is not what I expected.
The code I use for plotting the points the second time is:
figure()
for i=1:1000 %1000 is the length of x_vector and y_vector
plot(x_vector(i), y_vector(i), 'o')
hold on
axis([0 1920 0 1080]);
set(gca, 'ydir', 'reverse', 'xaxislocation', 'top')
end
And this is the result of the previous code:
I guess that the second time I try to plot the two vectors plot is trying to print some linear function through some regression instead of printing the cloud of points I expect.
Thank you

댓글 수: 11

madhan ravi
madhan ravi 2018년 8월 23일
편집: madhan ravi 2018년 8월 23일
Upload your datas too (x_vector and y_vector).
Stephan
Stephan 2018년 8월 23일
편집: Stephan 2018년 8월 23일
Hi,
Why do you use a loop for this?
scatter(x_vector, y_vector, 'or')
axis([0 1920 0 1080]);
set(gca, 'ydir', 'reverse', 'xaxislocation', 'top')
dpb
dpb 2018년 8월 23일
I think we would need to see how the x,y coordinates were stored and the original figure as well, not just the latter.
madhan ravi
madhan ravi 2018년 8월 23일
I second @dpb
I first created the two vectors:
x_vector = [];
y_vector = [];
Then, I stored the x and y the eyetracker sends:
x_vector = sort([x_vector x]);
y_vector = sort([y_vector y]);
And the original figure is the first one I posted on the question.
Ángel González
Ángel González 2018년 8월 23일
편집: Ángel González 2018년 8월 23일
I upload the two vectors saved from Matlab.
PS: By using scatter I get a similar result..
x_vector = sort([x_vector x]);
y_vector = sort([y_vector y]);
And that explains it...you sorted the points independent of each other so the association of x,y is broken.
@dpb, which solution would you use to storage the x and y values?
Thank you!
dpb
dpb 2018년 8월 23일
편집: dpb 2018년 8월 23일
Simply store the points as you receive them...you don't show code but
% preallocate
N=npts;
x_vector=zeros(npts,1);
y_vector=x_vector;
% collect, save...
for i=1:npts
% return x, y, here...
x_vector(i)=x;
y_vector(i)=y;
% do whatever else with x,y during download
...
end
presuming you know a priori how many points you're going to collect. If that is unknown, start with a big number and then remove those not needed.
Alternatively, while it's not efficient to code this way, the dynamic allocation as you wrote above is simple but overhead intensive--
x_vector=[]; y_vector=[];
while stilldownloading()
% get x, y
x_vector=[x_vector;x];
y_vector=[y_vector;y];
...
end
will keep each x,y pair together.
Maybe replace
x_vector = sort([x_vector x]);
y_vector = sort([y_vector y]);
by
[x_vector, index] = sort([x_vector x]);
yTemp = [y_vector y];
y_vector = yTemp(index);
That's calling sort repetitively every iteration...there's no indication of needing them sorted; if do, far more efficient to wait until done collecting and then sort the whole vector once.
Keeping and reordering y based on order of sorted x is, of course, necessry to keep the association between the two.
Alternatively, one could do slightly differently as
xy=[];
while collecting()
% get x, y
xy=[xy; [x y]];
...
end
xy=sortrows(xy);
and will have x,y in two columns in the one arrray sorted by increasing x

댓글을 달려면 로그인하십시오.

 채택된 답변

Ángel González
Ángel González 2018년 8월 23일

0 개 추천

Thank you all for helping me. As you said, the problem was how I was storing the coordinates. This comment from @dpb resumed the problem perfectly: https://es.mathworks.com/matlabcentral/answers/416020-how-can-i-plot-points-in-matlab#comment_602802

추가 답변 (1개)

Afshin Aghayan
Afshin Aghayan 2019년 10월 9일

0 개 추천

you can use this code for displaying any data in the form of [x, y, f(x,y)] or data with coordinate

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by