Imag, real function error

조회 수: 4 (최근 30일)
Sean Pitre
Sean Pitre 2017년 9월 12일
댓글: Star Strider 2017년 9월 12일
when I try to use the imag or real functions I get an error saying 'Subscript indices must either be real positive integers or logicals.' I don't get this when I use the isreal or abs functions. Why is this?
  댓글 수: 3
Sean Pitre
Sean Pitre 2017년 9월 12일
편집: Walter Roberson 2017년 9월 12일
Its a two line test code. Like,
X=2+3i
Y=real(x) or imag(x).
Even if I do it directly like Real(2+3i) I get the error
James Tursa
James Tursa 2017년 9월 12일
Try clearing all variables first, and then re-try your code to confirm that it is a variable naming issue or not.

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

답변 (2개)

James Tursa
James Tursa 2017년 9월 12일
편집: James Tursa 2017년 9월 12일
It could be that you have inadvertently created variables with the name "real" or "imag" that are shadowing the functions real and/or imag.

Star Strider
Star Strider 2017년 9월 12일
편집: Star Strider 2017년 9월 12일
My guess is that you used ‘imag’ and ‘real’ as variables somewhere earlier in your code. The solution is to find those variables and rename them. (The builtin (link) function can shed light on this.)
Example
imag = 42;
x = 1 + 2*j;
A1 = builtin('imag', x)
A2 = imag(x)
yields:
A1 =
2.0000e+000
For ‘A2’:
Subscript indices must either be real positive integers or logicals.
Error in FileName (line #####)
A2 = imag(x)
  댓글 수: 3
James Tursa
James Tursa 2017년 9월 12일
What does this show?
which real
which imag
Star Strider
Star Strider 2017년 9월 12일
The which function can be helpful.
Note all the results from:
which real -all
If the first result is:
real is a variable.
you’ve found the problem.

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

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by