i get this error "Subscript indices must either be real positive integers or logicals."
조회 수: 1 (최근 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에서 Argument Definitions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!