I have a complex number that I want to output as text using the fprintf command. I don\'t see a formatspec for complex numbers. Is there an easy way to do this?
Usin
According to the documentation of fprintf and sprintf
Numeric conversions print only the real component of complex numbers.
So for a complex value z
you can use this
sprintf('%f + %fi\n', z, z/1i)
for example
>> z=2+3i;
>> sprintf('%f + %fi\n', z, z/1i)
2.000000 + 3.000000i
You can wrap it in an anonymous function to get it easily as a string
zprintf = @(z) sprintf('%f + %fi', z, z/1i)
then
>> zprintf(2+3i)
ans =
2.000000 + 3.000000i