From: Armin Mueller on
Hi all,

what is the best way to parse a string like this:

-50 mm;0 mm;250 mm

I would like to get the three coordinates as numbers, e.g.

x = -50
y = 0
z = 250

I played around with sscanf(), but didn't get it working.

Thank you very much for any hint!
Armin
From: us on
Armin Mueller <arm.in(a)web.de> wrote in message <hqq736$a34$1(a)news2.rz.uni-karlsruhe.de>...
> Hi all,
>
> what is the best way to parse a string like this:
>
> -50 mm;0 mm;250 mm
>
> I would like to get the three coordinates as numbers, e.g.
>
> x = -50
> y = 0
> z = 250
>
> I played around with sscanf(), but didn't get it working.
>
> Thank you very much for any hint!
> Armin

one of the solutions

s='50 mm;0 mm;250 mm';
n=strread(s,'%f','whitespace',['m;'])
%{
% n =
50
0
250
%}

us
From: Walter Roberson on
Armin Mueller wrote:
> Hi all,
>
> what is the best way to parse a string like this:
>
> -50 mm;0 mm;250 mm
>
> I would like to get the three coordinates as numbers, e.g.
>
> x = -50
> y = 0
> z = 250

str2num(char(regexp(String, '-?\d+', 'match')))

will give all of them, as numbers, in a single pass.
From: Armin Mueller on
us wrote:

> one of the solutions [...]

Urs, you are brilliant. Thank you! - Armin
From: Jan Simon on
Dear Armin!

> what is the best way to parse a string like this:
> -50 mm;0 mm;250 mm
>
> I would like to get the three coordinates as numbers, e.g.
> x = -50
> y = 0
> z = 250
>
> I played around with sscanf(), but didn't get it working.

To be complete here the SSCANF method:
Str = '-50 mm; 0 mm; 250 mm';
X = sscanf(Str, '%d mm;', 3)
==> X = [-50; 0; 250]

Good luck, Jan