Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Printing complex numbers with fprintf

Subject: Printing complex numbers with fprintf

From: Chris Severns

Date: 25 Apr, 2005 18:06:25

Message: 1 of 6

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.

Subject: Printing complex numbers with fprintf

From: Murphy O'Brien

Date: 25 Apr, 2005 19:26:53

Message: 2 of 6

Chris Severns wrote:
>
>
> 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.

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

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

Murphy

Subject: Printing complex numbers with fprintf

From: Dan Hensley

Date: 25 Apr, 2005 17:33:16

Message: 3 of 6

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

Subject: Printing complex numbers with fprintf

From: Abas

Date: 27 Apr, 2005 01:01:52

Message: 4 of 6

Chris Severns wrote:
>
>
> 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,
You can use 'disp' function instead of 'fprintf'.
'disp' display a complex number, but 'fprintf' show only the real
part.

Subject: Printing complex numbers with fprintf

From: James

Date: 29 May, 2013 09:46:11

Message: 5 of 6

"Chris Severns" <christopher.m.severns@boeing.com> wrote in message <ef03fbd.-1@webx.raydaftYaTP>...
> 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

Subject: Printing complex numbers with fprintf

From: Rick

Date: 28 Nov, 2014 18:54:06

Message: 6 of 6

"James " <jc211@ic.ac.uk> wrote in message <ko4ip3$f00$1@newscl01ah.mathworks.com>...
> "Chris Severns" <christopher.m.severns@boeing.com> wrote in message <ef03fbd.-1@webx.raydaftYaTP>...
> > 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

This solution works perfectly. Thanks for contributing this!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us