How can I plot a series of points in a vector with two coordinates x&y considering that I want to name these points respectively S1, S2....S10, and I want to use them in other functions just by using their "name"?
조회 수: 1 (최근 30일)
이전 댓글 표시
I want to plot a series of points in a vector with two coordinates x&y and I want to name these points respectively S1, S2....S10, and then I wantto use them in other functions just by using their "name". How can I write the code to be able to do this? The vector will be: S=[S1,S2,S3,S4,S5,S6,S7,S8,S9,S10] While the coordinates will be: x=[0 0.5 1 0.7 -1 -0.5 0.5 0.5 -0.5]; y=[1 -1 1 0.5 0 0.8 0.8 0 -0.8 -0.8]; I want to plot them with the respective letters, and then use them in functions just by typing the letter. Thank you in advance.
댓글 수: 1
답변 (1개)
Jan
2018년 1월 13일
This sounds like a rather complicated and indirect way of programming. Of course your could simply create a set of variables:
S1 = [0, 1]
S2 = [0.5, -1]
...
But as you find in many (hundreds!) discussions in this forum, this way is known to cause more troubles than it solves. Hiding indices in names of variables is not efficient, unclear and deprecated:
- https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
- https://www.mathworks.com/matlabcentral/answers/57445-faq-how-can-i-create-variables-a1-a2-a10-in-a-loop
Using vectors and indices is the standard method and there are good reasons for this.
But to be honest, I'm not sure, what this exactly means:
I want to plot them with the respective letters, and then use them
in functions just by typing the letter
댓글 수: 8
Jan
2018년 1월 14일
편집: Jan
2018년 1월 14일
@Jenny: If you post your code, the readers can suggest improvements. Sometimes it is easier to fix an existing code than to write it completely from scratch. Then we could e.g. know, how your inputs look like. Do you get the position of the other sensors as an array? Or do you use random points? Or an even grid of coordinates?
What about the code I had suggested already - with a tiny modification:
R = 1.0;
dist2 = sqrt(x .^ 2 + y .^ 2);
match = dist2 - R < 0.08; % Or abs(dist2 - R) < 0.08 ??
xm = x(match)
ym = y(match)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Preprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!