Thread Subject: get numeric values

Subject: get numeric values

From: recep mansiz

Date: 5 Oct, 2009 06:49:01

Message: 1 of 5

Hi all!!

How can I fetch the numeric values from a string like

'x=-12 y=23 z=-345'

I want to fetch -12, 23 and -345

Thanks in advance!!

Subject: get numeric values

From: James Tursa

Date: 5 Oct, 2009 07:11:02

Message: 2 of 5

"recep mansiz" <aalmoraa@hotmail.com> wrote in message <hac4st$m6h$1@fred.mathworks.com>...
> Hi all!!
>
> How can I fetch the numeric values from a string like
>
> 'x=-12 y=23 z=-345'
>
> I want to fetch -12, 23 and -345
>
> Thanks in advance!!

If there are no spaces within the expressions, just between the expressions, you could do this:

s = 'x=-12 y=23 z=-345'
s(s==' ')=';'
eval(s)

James Tursa

Subject: get numeric values

From: recep mansiz

Date: 5 Oct, 2009 07:50:03

Message: 3 of 5

thanks very much. it works better than I planned.

Subject: get numeric values

From: Jan Simon

Date: 5 Oct, 2009 08:08:01

Message: 4 of 5

Dear recep mansiz!

> How can I fetch the numeric values from a string like
> 'x=-12 y=23 z=-345'
> I want to fetch -12, 23 and -345

Another idea:
  Str = 'x=-12 y=23 z=-345';
  EqIndex = findstr(Str, '=');
  Numbers = zeros(1, length(EqIndex));
  for iNumber = 1:length(Numbers)
    Numbers(iNumber) = sscanf(Str(EqIndex(iNumber) + 1:end), '%d', 1);
  end

Good luck, Jan

Subject: get numeric values

From: Jos

Date: 5 Oct, 2009 09:02:03

Message: 5 of 5

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hac666$fdj$1@fred.mathworks.com>...
> "recep mansiz" <aalmoraa@hotmail.com> wrote in message <hac4st$m6h$1@fred.mathworks.com>...
> > Hi all!!
> >
> > How can I fetch the numeric values from a string like
> >
> > 'x=-12 y=23 z=-345'
> >
> > I want to fetch -12, 23 and -345
> >
> > Thanks in advance!!
>
> If there are no spaces within the expressions, just between the expressions, you could do this:
>
> s = 'x=-12 y=23 z=-345'
> s(s==' ')=';'
> eval(s)
>
> James Tursa

Using EVAL, you should be very sure that string your string does not contain any malicious code ...

Here is another approach:
STR = 'x=-12 y=23 z=-345'
V = strread(STR,'%*c=%f')

Jos

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com