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 the % symbol

Subject: Printing the % symbol

From: CNN

Date: 27 Jul, 2010 15:15:38

Message: 1 of 8

Good day
I'm trying to get the percentage sign printed in a string in my m-file. So I tried to test my options and I'm not getting the answer I expected. Could anyone tell me what I doing wrong here?
str1 = {num2str(sum(a),'%%')}
or
str1 = sprintf('%%',sum(a))

where a = 1:2:20

I just need str1 to contain one value '100%'

Thanks in advance.

Subject: Printing the % symbol

From: Jim Rockford

Date: 27 Jul, 2010 15:18:57

Message: 2 of 8

On Jul 27, 11:15 am, "CNN " <cnln2...@yahoo.co.uk> wrote:
> Good day
> I'm trying to get the percentage sign printed in a string in my m-file.  So I tried to test my options and I'm not getting the answer I expected. Could anyone tell me what I doing wrong here?
> str1 = {num2str(sum(a),'%%')}
> or
> str1 = sprintf('%%',sum(a))
>
> where a = 1:2:20
>
> I just need str1 to contain one value '100%'
>
> Thanks in advance.


How about

str1=strcat(num2str(sum(a)),'%')

Subject: Printing the % symbol

From: Rick Branch

Date: 27 Jul, 2010 15:39:19

Message: 3 of 8

On Jul 27, 11:15 am, "CNN " <cnln2...@yahoo.co.uk> wrote:
> Good day
> I'm trying to get the percentage sign printed in a string in my m-file.  So I tried to test my options and I'm not getting the answer I expected. Could anyone tell me what I doing wrong here?
> str1 = {num2str(sum(a),'%%')}
> or
> str1 = sprintf('%%',sum(a))
>
> where a = 1:2:20
>
> I just need str1 to contain one value '100%'
>
> Thanks in advance.

sprintf('%d%%', sum(a))

Subject: Printing the % symbol

From: CNN

Date: 27 Jul, 2010 20:16:26

Message: 4 of 8

Rick Branch <rbranch6364@gmail.com> wrote in message <36681641-ae66-474d-8756-877c12fb1603@e35g2000vbl.googlegroups.com>...
> On Jul 27, 11:15 am, "CNN " <cnln2...@yahoo.co.uk> wrote:
> > Good day
> > I'm trying to get the percentage sign printed in a string in my m-file.  So I tried to test my options and I'm not getting the answer I expected. Could anyone tell me what I doing wrong here?
> > str1 = {num2str(sum(a),'%%')}
> > or
> > str1 = sprintf('%%',sum(a))
> >
> > where a = 1:2:20
> >
> > I just need str1 to contain one value '100%'
> >
> > Thanks in advance.
>
> sprintf('%d%%', sum(a))

Thanks, both answers worked. But I don't understand the format '%d%%'. I know '%d' formats 'sum(a)' as an integer so do the remaining %% provide the percentage sign? If that is the case, why didn't it work for me? Do I really have to indicate that it is an integer first?

Subject: Printing the % symbol

From: us

Date: 27 Jul, 2010 20:24:03

Message: 5 of 8

On Jul 27, 10:16 pm, "CNN " <cnln2...@yahoo.co.uk> wrote:
> Rick Branch <rbranch6...@gmail.com> wrote in message <36681641-ae66-474d-8756-877c12fb1...@e35g2000vbl.googlegroups.com>...
> > On Jul 27, 11:15 am, "CNN " <cnln2...@yahoo.co.uk> wrote:
> > > Good day
> > > I'm trying to get the percentage sign printed in a string in my m-file.  So I tried to test my options and I'm not getting the answer I expected. Could anyone tell me what I doing wrong here?
> > > str1 = {num2str(sum(a),'%%')}
> > > or
> > > str1 = sprintf('%%',sum(a))
>
> > > where a = 1:2:20
>
> > > I just need str1 to contain one value '100%'
>
> > > Thanks in advance.
>
> > sprintf('%d%%', sum(a))
>
> Thanks, both answers worked. But I don't understand the format '%d%%'. I know '%d' formats 'sum(a)' as an integer so do the remaining %% provide the percentage sign? If that is the case, why didn't it work for me? Do I really have to indicate that it is an integer first?  

well...
YES... how could it possibly be otherwise(?)... a built-in, magic
thought-reader (albeit, CSSMers are still waiting for that particular
tbx by steve eddins)...

more wisdom may be collected by typing this into the command window

     doc sprintf;

us

Subject: Printing the % symbol

From: Walter Roberson

Date: 27 Jul, 2010 20:21:22

Message: 6 of 8

CNN wrote:

>> > str1 = sprintf('%%',sum(a))

That is a format for printing a literal percent sign and then ignoring the sum
argument.

>> sprintf('%d%%', sum(a))

> Thanks, both answers worked. But I don't understand the format '%d%%'. I
> know '%d' formats 'sum(a)' as an integer so do the remaining %% provide
> the percentage sign?

Yes.

> If that is the case, why didn't it work for me? Do
> I really have to indicate that it is an integer first?

Yes, you really have to indicate that it is an integer first. sprintf() only
converts the arguments that you instruct it to convert.

Subject: Printing the % symbol

From: Andy

Date: 27 Jul, 2010 20:27:08

Message: 7 of 8

"CNN " <cnln2000@yahoo.co.uk> wrote in message <i2neqq$rt5$1@fred.mathworks.com>...
> Rick Branch <rbranch6364@gmail.com> wrote in message <36681641-ae66-474d-8756-877c12fb1603@e35g2000vbl.googlegroups.com>...
> > On Jul 27, 11:15 am, "CNN " <cnln2...@yahoo.co.uk> wrote:
> > > Good day
> > > I'm trying to get the percentage sign printed in a string in my m-file.  So I tried to test my options and I'm not getting the answer I expected. Could anyone tell me what I doing wrong here?
> > > str1 = {num2str(sum(a),'%%')}
> > > or
> > > str1 = sprintf('%%',sum(a))
> > >
> > > where a = 1:2:20
> > >
> > > I just need str1 to contain one value '100%'
> > >
> > > Thanks in advance.
> >
> > sprintf('%d%%', sum(a))
>
> Thanks, both answers worked. But I don't understand the format '%d%%'. I know '%d' formats 'sum(a)' as an integer so do the remaining %% provide the percentage sign? If that is the case, why didn't it work for me? Do I really have to indicate that it is an integer first?

You have to indicate that it is an integer first only if you want it to appear first. You could have used:

str1 = sprintf('%%%d',sum(a))

This writes the percent symbol, then the sum(a). But your format string doesn't tell sprintf where to place sum(a) at all, so it just doesn't place it anywhere.

Subject: Printing the % symbol

From: Rohan

Date: 14 Aug, 2011 11:44:29

Message: 8 of 8

Just do this:

water = 70;
fprintf('this is %d %% of our planet surface',water);

%A double percentage symbol w/o spaces will be printed as "%" in MATLAB

Cheers
Ron

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