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:
strings vector using num2str

Subject: strings vector using num2str

From: RAB John

Date: 29 Aug, 2010 13:02:04

Message: 1 of 4

Hello all,

I wish to use text() and num2str() combination in order to text a graph in multiple point with out the use of a LOOP!
The only problem is num2str() producing one string instead of a strings vector.

The code I tried is:

text(1:2:5, abs(Y((1:2:5) + 1)), num2str(abs(Y((1:2:5) + 1))), '%1.3d'));

where Y is a line vector.

a simillar looped version would be:

for i = 1:2:5
    text(i, abs(Y(i + 1)), num2str(abs(Y(i + 1)), '%1.3d'));
end

Any suggestion?
DK

Subject: strings vector using num2str

From: Oleg Komarov

Date: 29 Aug, 2010 15:43:03

Message: 2 of 4

"RAB John" <dkkddkkd@walla.com> wrote in message <i5dloc$ks7$1@fred.mathworks.com>...
> Hello all,
>
> I wish to use text() and num2str() combination in order to text a graph in multiple point with out the use of a LOOP!
> The only problem is num2str() producing one string instead of a strings vector.
>
> The code I tried is:
>
> text(1:2:5, abs(Y((1:2:5) + 1)), num2str(abs(Y((1:2:5) + 1))), '%1.3d'));
>
> where Y is a line vector.
>
> a simillar looped version would be:
>
> for i = 1:2:5
> text(i, abs(Y(i + 1)), num2str(abs(Y(i + 1)), '%1.3d'));
> end
>
> Any suggestion?
> DK

Try this one:
text(1:3, 1:3, num2str(1:3, '%d').');
set(gca,'Xlim',[0 3]);
set(gca,'Ylim',[0 3]);

Note the .' to obtain a column vector after num2str. I also would use
sprintf('%d',1:3).'

Oleg

Subject: strings vector using num2str

From: RAB John

Date: 29 Aug, 2010 20:04:03

Message: 3 of 4

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <i5dv67$ie9$1@fred.mathworks.com>...
> "RAB John" <dkkddkkd@walla.com> wrote in message <i5dloc$ks7$1@fred.mathworks.com>...
> > Hello all,
> >
> > I wish to use text() and num2str() combination in order to text a graph in multiple point with out the use of a LOOP!
> > The only problem is num2str() producing one string instead of a strings vector.
> >
> > The code I tried is:
> >
> > text(1:2:5, abs(Y((1:2:5) + 1)), num2str(abs(Y((1:2:5) + 1))), '%1.3d'));
> >
> > where Y is a line vector.
> >
> > a simillar looped version would be:
> >
> > for i = 1:2:5
> > text(i, abs(Y(i + 1)), num2str(abs(Y(i + 1)), '%1.3d'));
> > end
> >
> > Any suggestion?
> > DK
>
> Try this one:
> text(1:3, 1:3, num2str(1:3, '%d').');
> set(gca,'Xlim',[0 3]);
> set(gca,'Ylim',[0 3]);
>
> Note the .' to obtain a column vector after num2str. I also would use
> sprintf('%d',1:3).'
>
> Oleg

Thx for the quick response!

Though it works fine the way you put it, it doesn't goes well when I add a function to the basic 1:3 vector.
I tried the following:

text(1:2:5, abs(Y((1:2:5) + 1)), num2str(Y(1:2:5), '%d').');

and

text(1:2:5, abs(Y((1:2:5) + 1)), num2str(abs(Y(1:2:5)), '%d').');

but both didn't work.

Any comments/suggestions?

RAB

Subject: strings vector using num2str

From: Oleg Komarov

Date: 30 Aug, 2010 07:31:03

Message: 4 of 4

> Thx for the quick response!
>
> Though it works fine the way you put it, it doesn't goes well when I add a function to the basic 1:3 vector.
> I tried the following:
>
> text(1:2:5, abs(Y((1:2:5) + 1)), num2str(Y(1:2:5), '%d').');
>
> and
>
> text(1:2:5, abs(Y((1:2:5) + 1)), num2str(abs(Y(1:2:5)), '%d').');
>
> but both didn't work.
>
> Any comments/suggestions?
>
> RAB

I don't know what exactly didn't work according to your expectations.
If Y(1:2:5) are numbers with more than one digit than you should transpose it first before converting into string.

Y = [10, 22.5];
num2str(Y.','%f')
ans =
10.0
22.5

Oleg

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