Imag, real function error

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 comentarios

James Tursa
James Tursa el 12 de Sept. de 2017
Please post your code that generates this error.
Sean Pitre
Sean Pitre el 12 de Sept. de 2017
Editada: Walter Roberson el 12 de Sept. de 2017
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 el 12 de Sept. de 2017
Try clearing all variables first, and then re-try your code to confirm that it is a variable naming issue or not.

Iniciar sesión para comentar.

Respuestas (2)

James Tursa
James Tursa el 12 de Sept. de 2017
Editada: James Tursa el 12 de Sept. de 2017

0 votos

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 el 12 de Sept. de 2017
Editada: Star Strider el 12 de Sept. de 2017

0 votos

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 comentarios

Sean Pitre
Sean Pitre el 12 de Sept. de 2017
The code I have is two lines long. I just declare a variable x as some complex number then try to get the real part of that number. I'll check again to see if maybe I missed something. My code is just x = 2 + 3i y = real(x)
James Tursa
James Tursa el 12 de Sept. de 2017
What does this show?
which real
which imag
Star Strider
Star Strider el 12 de Sept. de 2017
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Sept. de 2017

Comentada:

el 12 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by