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!