From: us on
"Mike " <mikejcunningham(a)gmail.com> wrote in message <i2saca$74c$1(a)fred.mathworks.com>...
> I am trying to read some numbers that are in scientific notation out of a string.
>
> An example string is:
>
> # Beam sigy = 0 m, muy = -2e-06 m, dy = 1e-06 m
>
> where I need to read out:
>
> 0
> -2e-06
> 1e-06
>
> I have not been able to find a way to do this. I have played with textscan and regexp but no luck. Maybe someone has an idea?
>
> Thanks,
> --
> Mike

one of the many solutions
- based on the above format

s='# Beam sigy = 0 m, muy = -2e-06 m, dy = 1e-06 m';
r=regexp(s,'(?<== )((-)|(\d+)|(e))+','match');
r=sscanf(sprintf('%s ',r{:}),'%g')
%{
% r =
0
-2e-006
1e-006
%}

us
From: us on
"Mike " <mikejcunningham(a)gmail.com> wrote in message <i2sd0o$eo$1(a)fred.mathworks.com>...
> Walter Roberson <roberson(a)hushmail.com> wrote in message <Ari4o.4420$mW5.1426(a)newsfe14.iad>...
> > Mike wrote:
> > > I am trying to read some numbers that are in scientific notation out of
> > > a string.
> > >
> > > An example string is:
> > >
> > > # Beam sigy = 0 m, muy = -2e-06 m, dy = 1e-06 m
> > >
> > > where I need to read out:
> > >
> > > 0
> > > -2e-06
> > > 1e-06
> > >
> > > I have not been able to find a way to do this. I have played with
> > > textscan and regexp but no luck. Maybe someone has an idea?
> >
> > Try a scan format of '%*[^=]%*[=]%f%*[m]'
>
> I tried doing textscan(line, '%*[^=]%*[=]%f%*[m]') and I get an output of
>
> ans = [3x1 double]
>
> not sure what the issue is and I also don't understand what your format is saying/doing?

well... you're just using

one of the other solutions

s='# Beam sigy = 0 m, muy = -2e-06 m, dy = 1e-06 m';
r=textscan(s,'%*[^=]%*[=]%f%*[m]');
r=[r{:}]
%{
% r =
0
-2e-006
1e-006
%}

us
From: Walter Roberson on
Mike wrote:
> Walter Roberson <roberson(a)hushmail.com> wrote in message
> <Ari4o.4420$mW5.1426(a)newsfe14.iad>...
>> Mike wrote:
>> > I am trying to read some numbers that are in scientific notation out
>> of > a string.
>> > > An example string is:
>> > > # Beam sigy = 0 m, muy = -2e-06 m, dy = 1e-06 m
>> > > where I need to read out:
>> > > 0
>> > -2e-06
>> > 1e-06
>> > > I have not been able to find a way to do this. I have played with
>> > textscan and regexp but no luck. Maybe someone has an idea?
>>
>> Try a scan format of '%*[^=]%*[=]%f%*[m]'
>
> I tried doing textscan(line, '%*[^=]%*[=]%f%*[m]') and I get an output of
>
> ans = [3x1 double]
>
> not sure what the issue is and I also don't understand what your format
> is saying/doing?

As Urs indicated, you just need to de-reference the cell that textscan produces.

%*[^=]

means to scan for a class of characters (the %[] part), where the class of
characters is everything _except_ (the ^ part) the equal-sign (the = part),
and then to throw the scanned value away (the * part).

%*[=] means to scan for equal signs and to throw the scanned value away.

Between these two, you scan up to and including the = and discard that part.

The %f reads a floating point number. Because there is no * the result is
retained.

The %*[m] scans for 'm' and discards it.


Now that I'm at my desk I have confirmed that you can do the same thing with

%*[^=]=%fm

as literal characters are automatically discarded when matched.
From: Mike on
Walter Roberson <roberson(a)hushmail.com> wrote in message <i2sg53$s83$1(a)canopus.cc.umanitoba.ca>...
> Mike wrote:
> > Walter Roberson <roberson(a)hushmail.com> wrote in message
> > <Ari4o.4420$mW5.1426(a)newsfe14.iad>...
> >> Mike wrote:
> >> > I am trying to read some numbers that are in scientific notation out
> >> of > a string.
> >> > > An example string is:
> >> > > # Beam sigy = 0 m, muy = -2e-06 m, dy = 1e-06 m
> >> > > where I need to read out:
> >> > > 0
> >> > -2e-06
> >> > 1e-06
> >> > > I have not been able to find a way to do this. I have played with
> >> > textscan and regexp but no luck. Maybe someone has an idea?
> >>
> >> Try a scan format of '%*[^=]%*[=]%f%*[m]'
> >
> > I tried doing textscan(line, '%*[^=]%*[=]%f%*[m]') and I get an output of
> >
> > ans = [3x1 double]
> >
> > not sure what the issue is and I also don't understand what your format
> > is saying/doing?
>
> As Urs indicated, you just need to de-reference the cell that textscan produces.
>
> %*[^=]
>
> means to scan for a class of characters (the %[] part), where the class of
> characters is everything _except_ (the ^ part) the equal-sign (the = part),
> and then to throw the scanned value away (the * part).
>
> %*[=] means to scan for equal signs and to throw the scanned value away.
>
> Between these two, you scan up to and including the = and discard that part.
>
> The %f reads a floating point number. Because there is no * the result is
> retained.
>
> The %*[m] scans for 'm' and discards it.
>
>
> Now that I'm at my desk I have confirmed that you can do the same thing with
>
> %*[^=]=%fm
>
> as literal characters are automatically discarded when matched.

Ok thanks guys. US's suggestion worked. I tried to get the other but didn't succeed. Thank though.
From: Mike on
So using US's method I can read in a line that looks like:

# Beam sigy = 0 m, muy = -1e-05 m, dy = 1e-06 m

However, something I didn't realise at the time, some files have the string looking like:

# Beam sigy = 0 m, muy = -0.000396 m, dy = 1e-06 m
OR
# Beam sigy = 0 m, muy = -5.6e-05 m, dy = 1e-06 m

For the first it will return -0 for the middle number
and
for the 2nd it will return -5 and leave for the middle number.

I'm using

r=regexp(line,'(?<== )((-)|(\d+)|(e))+','match')
r=sscanf(sprintf('%s ',r{:}),'%g');

is there a way to make it able to read these 3 formats?

Thanks again,
--
Mike