Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Printing complex numbers with fprintf

498 views
Skip to first unread message

Chris Severns

unread,
Apr 25, 2005, 6:06:25 PM4/25/05
to
I'm attempting to print a complex number (x+j*y) using fprintf. I'm
only getting the real componet when I use %f or %g (e.g.
fprintf('%1.4g \n', z).

Any ideas?

perhaps I need to print the real and then the imginary in two parts:
fprintf('%1.4g %1.4g \n', real(z), imag(z))

but is there a way to avoid this?

Thanks.

Murphy O'Brien

unread,
Apr 25, 2005, 7:26:53 PM4/25/05
to

Not that I know of. I would probably do this though.

fprintf('%5.4g + j%5.4g\n', real(z), imag(z))

Murphy

Dan Hensley

unread,
Apr 25, 2005, 7:33:16 PM4/25/05
to
On Mon, 25 Apr 2005 19:26:53 -0400, Murphy O'Brien wrote:

> Not that I know of. I would probably do this though.
>
> fprintf('%5.4g + j%5.4g\n', real(z), imag(z))

If z is a vector, do this:

fprintf('%5.4g + j%5.4g\n', [real(z) imag(z)].');

Dan

Abas

unread,
Apr 27, 2005, 1:01:52 AM4/27/05
to

Hi,
You can use 'disp' function instead of 'fprintf'.
'disp' display a complex number, but 'fprintf' show only the real
part.

James

unread,
May 29, 2013, 5:46:11 AM5/29/13
to
"Chris Severns" <christophe...@boeing.com> wrote in message <ef03f...@webx.raydaftYaTP>...
Hi Chris

This is 8 years late but perhaps still of use to some people!

Try

fprintf( '%s \n' , num2str(z) )

J

Rick

unread,
Nov 28, 2014, 1:54:12 PM11/28/14
to
"James " <jc...@ic.ac.uk> wrote in message <ko4ip3$f00$1...@newscl01ah.mathworks.com>...
This solution works perfectly. Thanks for contributing this!

Karun

unread,
Jan 8, 2015, 4:17:11 PM1/8/15
to
"James " <jc...@ic.ac.uk> wrote in message <ko4ip3$f00$1...@newscl01ah.mathworks.com>...
That worked perfectly my man! Have a 2015 thank you

Honigmelone

unread,
Mar 21, 2016, 10:56:10 AM3/21/16
to
> > I'm attempting to print a complex number (x+j*y) using fprintf. I'm
> > only getting the real componet when I use %f or %g (e.g.
> > fprintf('%1.4g \n', z).
> >
> > Any ideas?
> >
> > perhaps I need to print the real and then the imginary in two parts:
> > fprintf('%1.4g %1.4g \n', real(z), imag(z))
> >
> > but is there a way to avoid this?
> >
> > Thanks.
>
> Hi Chris
>
> This is 8 years late but perhaps still of use to some people!
>
> Try
>
> fprintf( '%s \n' , num2str(z) )
>
> J
Hey, when I try this with row vectors containing complex numbers I get a strange result:
fprintf('%s\n',num2str([sqrt(-2);sqrt(-2)]))
00++11..44114422ii
whereas
num2str([sqrt(-2);sqrt(-2)])
ans =
0+1.4142i
0+1.4142i

Any Idea what I've done wrong? Thanks!

Bruno Luong

unread,
Mar 21, 2016, 11:16:09 AM3/21/16
to
num2str returns an array, and the characters are aranged columnwise. If you don't know what I'm talking about just apply blindly this:

>> c=cellstr(num2str([sqrt(-2);sqrt(-2)]))

c =

'0+1.4142i'
'0+1.4142i'

>> fprintf('%s\n',c{:})
0+1.4142i
0+1.4142i

Steven Lord

unread,
Mar 22, 2016, 9:35:33 AM3/22/16
to


"Honigmelone" <Math...@alshu.de> wrote in message
news:ncp224$hcu$1...@newscl01ah.mathworks.com...
Elements in matrices are stored in column major order.

A = reshape(1:16, 4, 4)

Note how the elements of A increase down each column first, then once the
column is full the next element starts at the top of the next column? The
same holds for character matrices:

B = reshape('a':'z', 2, 13)

Some other languages are row major:

C = reshape('a':'z', 13, 2).'

https://en.wikipedia.org/wiki/Row-major_order

When you call NUM2STR on a column vector, the result will be a char matrix
with a number of rows equal to the number of rows in the column vector.

D = num2str(sqrt([-2; -2]))
size(D)
class(D)

When FPRINTF prints D, it walks that matrix in column order.

fprintf('%s', D)

You can see this a little easier with a different vector, where the two rows
aren't identical:

E = num2str(sqrt([-2; -7]))
F = sprintf('%s', E)
G = F(1:2:end)
H = F(2:2:end)

G matches the first row in E while H matches the second.

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

0 new messages