i get this error "Subscript indices must either be real positive integers or logicals."
3 次查看(过去 30 天)
显示 更早的评论
When i run this code, i get the above error.
Subscript indices must either be real positive integers or logicals.
Error in M_PSK_wrapper (line 19)
plot(EbN0dB,log10(simulatedSER(i,:)),sprintf('%s-',plotColor(j))); hold on;
0 个评论
回答(1 个)
Walter Roberson
2016-3-19
Your code has
%Plot commands j=1;
Up to that point, j has not been given a value in the code. That line is a comment so nothing happens with it. You then have
for i=M,
plot(EbN0dB,log10(simulatedSER(i,:)),sprintf('%s-',plotColor(j))); hold on;
j=j+1; end
The "for i" line does not assign a value to j. So by the time the plot() call is made, j has not been given a value in the code. You might have expected that to result in an error message about undefined function or variable j, but notice that I said that your code has not given a value to j, not that j is undefined. In MATLAB, i and j are both functions with no inputs, which return complex(0,1), which is sqrt(-1), the imaginary unit. When you assign a value to i or j, you are "overshadowing" that meaning as functions, making them become variables in that scope until that meaning gets released (such as if a "clear" is encountered.) And since you did not give j a value in your code, j is still that function that returns sqrt(-1) . Which, as a complex number, is not a valid subscript.
What you probably wanted was:
%Plot commands
j=1;
on two different lines. Then j would have been assigned a value.
To avoid this situation, experienced MATLAB programmers avoid using "i" and "j" as variable names. Depending on their background, they might avoid using "I" as well (it can refer to sqrt(-1) in symbolic work.)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!