From: Walter Roberson on
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.
From: Andy on
"CNN " <cnln2000(a)yahoo.co.uk> wrote in message <i2neqq$rt5$1(a)fred.mathworks.com>...
> Rick Branch <rbranch6364(a)gmail.com> wrote in message <36681641-ae66-474d-8756-877c12fb1603(a)e35g2000vbl.googlegroups.com>...
> > On Jul 27, 11:15 am, "CNN " <cnln2...(a)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.