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:
Help using "disp"

Subject: Help using "disp"

From: Daniel Eliasson

Date: 23 Jan, 2009 09:24:02

Message: 1 of 9

Hello

I have done a calcualtion and I get this

ans=

11.3

But I want it to say

The answer is: 11.3

How do I do that with the disp function?

Thanks

Subject: Help using "disp"

From: P

Date: 23 Jan, 2009 09:30:04

Message: 2 of 9

"Daniel Eliasson" <danielel@kth.se> wrote in message <glc2bi$3ee$1@fred.mathworks.com>...
> Hello
>
> I have done a calcualtion and I get this
>
> ans=
>
> 11.3
>
> But I want it to say
>
> The answer is: 11.3
>
> How do I do that with the disp function?
>
> Thanks

I come from a C background so I always do this


ans = 11.3;
fprintf('The answer is: %3.1f\n', ans);

Subject: Help using "disp"

From: Daniel Eliasson

Date: 23 Jan, 2009 10:09:02

Message: 3 of 9

"P" <pyjunk.nospam@shaw.ca> wrote in message <glc2ms$pgd$1@fred.mathworks.com>...
> "Daniel Eliasson" <danielel@kth.se> wrote in message <glc2bi$3ee$1@fred.mathworks.com>...
> > Hello
> >
> > I have done a calcualtion and I get this
> >
> > ans=
> >
> > 11.3
> >
> > But I want it to say
> >
> > The answer is: 11.3
> >
> > How do I do that with the disp function?
> >
> > Thanks
>
> I come from a C background so I always do this
>
>
> ans = 11.3;
> fprintf('The answer is: %3.1f\n', ans);


Thanks, but sometimes it gives "The answer is: 0.0" even if its not 0
my example:

I write this
UtBrd=Bredning*InBrd
fprintf('Wu width: %3.1f\n', UtBrd);

and this happens
UtBrd=
0.0309

Wu Width: 0.0

Is there something with the decimals?

Subject: Help using "disp"

From: Justus Skorps

Date: 23 Jan, 2009 10:14:26

Message: 4 of 9

On 23 Jan., 11:09, "Daniel Eliasson" <danie...@kth.se> wrote:
> "P" <pyjunk.nos...@shaw.ca> wrote in message <glc2ms$pg...@fred.mathworks.com>...
> > "Daniel Eliasson" <danie...@kth.se> wrote in message <glc2bi$3e...@fred.mathworks.com>...
> > > Hello
>
> > > I have done a calcualtion and I get this
>
> > > ans=
>
> > > 11.3
>
> > > But I want it to say
>
> > > The answer is: 11.3
>
> > > How do I do that with the disp function?
>
> > > Thanks
>
> > I come from a C background so I always do this
>
> > ans = 11.3;
> > fprintf('The answer is: %3.1f\n', ans);
>
> Thanks, but sometimes it gives "The answer is: 0.0" even if its not 0
> my example:
>
> I write this
> UtBrd=Bredning*InBrd
> fprintf('Wu width: %3.1f\n', UtBrd);
>
> and this happens
> UtBrd=
> 0.0309
>
> Wu Width: 0.0
>
> Is there something with the decimals?

perhaps you should look in matlab help what the fprintf parameters
do...

Subject: Help using "disp"

From: P

Date: 23 Jan, 2009 10:32:01

Message: 5 of 9

Hi again,

fprintf is very powerful but it does sometime to get use to syntax.

The magic format string '%3.1f' means 3 things

1. 3 is Field Width, 'A digit string specifying the minimum number of digits to be printed'
2. 1 is Precision, 'A digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point'
3. f implies fixed-point notation.

So yes you need to change the 1 to get to the other decimal. The %x.y formatting allows you to line up strings very nicely.

a=101
b=0.00001
fprintf('THIS IS %10.5f\n',a)
fprintf('THIS IS %10.5f\n',b)

Subject: Help using "disp"

From: Daniel Eliasson

Date: 23 Jan, 2009 10:37:02

Message: 6 of 9

"P" <pyjunk.nospam@shaw.ca> wrote in message <glc6b1$fr7$1@fred.mathworks.com>...
> Hi again,
>
> fprintf is very powerful but it does sometime to get use to syntax.
>
> The magic format string '%3.1f' means 3 things
>
> 1. 3 is Field Width, 'A digit string specifying the minimum number of digits to be printed'
> 2. 1 is Precision, 'A digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point'
> 3. f implies fixed-point notation.
>
> So yes you need to change the 1 to get to the other decimal. The %x.y formatting allows you to line up strings very nicely.
>
> a=101
> b=0.00001
> fprintf('THIS IS %10.5f\n',a)
> fprintf('THIS IS %10.5f\n',b)


Thank you very much!

Subject: Help using "disp"

From: Thomas Clark

Date: 23 Jan, 2009 13:49:02

Message: 7 of 9

fprintf is a pain. For generic formatting, use:

disp(['The answer is: ' num2str(ans)])

- Tom



"Daniel Eliasson" <danielel@kth.se> wrote in message <glc6ke$4gn$1@fred.mathworks.com>...
> "P" <pyjunk.nospam@shaw.ca> wrote in message <glc6b1$fr7$1@fred.mathworks.com>...
> > Hi again,
> >
> > fprintf is very powerful but it does sometime to get use to syntax.
> >
> > The magic format string '%3.1f' means 3 things
> >
> > 1. 3 is Field Width, 'A digit string specifying the minimum number of digits to be printed'
> > 2. 1 is Precision, 'A digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point'
> > 3. f implies fixed-point notation.
> >
> > So yes you need to change the 1 to get to the other decimal. The %x.y formatting allows you to line up strings very nicely.
> >
> > a=101
> > b=0.00001
> > fprintf('THIS IS %10.5f\n',a)
> > fprintf('THIS IS %10.5f\n',b)
>
>
> Thank you very much!
>

Subject: Help using "disp"

From: Andrew

Date: 23 Jan, 2009 14:04:50

Message: 8 of 9

On Jan 23, 6:49=A0am, "Thomas Clark" <t.cl...@remove.spamcantab.net>
wrote:
> fprintf is a pain. For generic formatting, use:
>
> disp(['The answer is: ' num2str(ans)])
>
> - Tom
>
> "Daniel Eliasson" <danie...@kth.se> wrote in message <glc6ke$4g...@fred.m=
athworks.com>...
> > "P" <pyjunk.nos...@shaw.ca> wrote in message <glc6b1$fr...@fred.mathwor=
ks.com>...
> > > Hi again,
>
> > > fprintf is very powerful but it does sometime to get use to syntax.
>
> > > The magic format string '%3.1f' means 3 things
>
> > > 1. 3 is Field Width, 'A digit string specifying the minimum number of=
 digits to be printed'
> > > 2. 1 is Precision, 'A digit string including a period (.) specifying =
the number of digits to be printed to the right of the decimal point'
> > > 3. f implies fixed-point notation.
>
> > > So yes you need to change the 1 to get to the other decimal. The %x.y=
 formatting allows you to line up strings very nicely.
>
> > > a=3D101
> > > b=3D0.00001
> > > fprintf('THIS IS %10.5f\n',a)
> > > fprintf('THIS IS %10.5f\n',b)
>
> > Thank you very much!

How would you format -- disp(['The answer is: ' num2str(ans)]) -- to
output a predetermined number of decimal places? I think fprintf is
great; but I also spend a lot of time in C.

Subject: Help using "disp"

From: Doug Schwarz

Date: 23 Jan, 2009 14:09:10

Message: 9 of 9

In article <glc6ke$4gn$1@fred.mathworks.com>,
 "Daniel Eliasson" <danielel@kth.se> wrote:

> "P" <pyjunk.nospam@shaw.ca> wrote in message
> <glc6b1$fr7$1@fred.mathworks.com>...
> > Hi again,
> >
> > fprintf is very powerful but it does sometime to get use to syntax.
> >
> > The magic format string '%3.1f' means 3 things
> >
> > 1. 3 is Field Width, 'A digit string specifying the minimum number of
> > digits to be printed'
> > 2. 1 is Precision, 'A digit string including a period (.) specifying the
> > number of digits to be printed to the right of the decimal point'
> > 3. f implies fixed-point notation.
> >
> > So yes you need to change the 1 to get to the other decimal. The %x.y
> > formatting allows you to line up strings very nicely.
> >
> > a=101
> > b=0.00001
> > fprintf('THIS IS %10.5f\n',a)
> > fprintf('THIS IS %10.5f\n',b)
>
>
> Thank you very much!

You might be happier with %g:

fprintf('The answer is %g.\n',x)

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

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