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
|
|
0
|
|
|
|
Reply
|
Mike
|
7/29/2010 4:31:06 PM |
|
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]'
|
|
0
|
|
|
|
Reply
|
Walter
|
7/29/2010 4:59:12 PM
|
|
Walter Roberson <roberson@hushmail.com> wrote in message <Ari4o.4420$mW5.1426@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?
|
|
0
|
|
|
|
Reply
|
Mike
|
7/29/2010 5:16:08 PM
|
|
"Mike " <mikejcunningham@gmail.com> wrote in message <i2saca$74c$1@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
|
|
0
|
|
|
|
Reply
|
us
|
7/29/2010 5:22:08 PM
|
|
"Mike " <mikejcunningham@gmail.com> wrote in message <i2sd0o$eo$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <Ari4o.4420$mW5.1426@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
|
|
0
|
|
|
|
Reply
|
us
|
7/29/2010 5:26:04 PM
|
|
Mike wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <Ari4o.4420$mW5.1426@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.
|
|
0
|
|
|
|
Reply
|
Walter
|
7/29/2010 6:05:31 PM
|
|
Walter Roberson <roberson@hushmail.com> wrote in message <i2sg53$s83$1@canopus.cc.umanitoba.ca>...
> Mike wrote:
> > Walter Roberson <roberson@hushmail.com> wrote in message
> > <Ari4o.4420$mW5.1426@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.
|
|
0
|
|
|
|
Reply
|
Mike
|
7/29/2010 6:53:04 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
Mike
|
7/29/2010 8:03:05 PM
|
|
Mike wrote:
> 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?
>> t = textscan('# Beam sigy = 0 m, muy = -0.000396 m, dy = 1e-06 m',
'%*[^=]=%fm',3);t{1}
ans =
0
-0.000396
1e-06
>> t = textscan('# Beam sigy = 0 m, muy = -5.6e-05 m, dy = 1e-06 m',
'%*[^=]=%fm',3);t{1}
ans =
0
-5.6e-05
1e-06
Getting a proper regexp format for scientific numbers is tricky.
|
|
0
|
|
|
|
Reply
|
Walter
|
7/29/2010 8:09:41 PM
|
|
"Mike " <mikejcunningham@gmail.com> wrote in message <i2smpp$h7k$1@fred.mathworks.com>...
> 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
of course...
one of the many solutions
s='# Beam sigy = 0 m, muy = -5.6e-05 m, dy = 1e-06 m';
r=regexp(s,'(?<== )((\d)|(-)|(\.)|(e))+','match');
r=sscanf(sprintf('%s ',r{:}),'%g')
%{
% r =
0
-5.6e-005
1e-006
%}
us
|
|
0
|
|
|
|
Reply
|
us
|
7/30/2010 12:33:05 PM
|
|
"us " <us@neurol.unizh.ch> wrote in message <i2ugq1$ajb$1@fred.mathworks.com>...
> "Mike " <mikejcunningham@gmail.com> wrote in message <i2smpp$h7k$1@fred.mathworks.com>...
> > 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
>
> of course...
>
> one of the many solutions
>
> s='# Beam sigy = 0 m, muy = -5.6e-05 m, dy = 1e-06 m';
> r=regexp(s,'(?<== )((\d)|(-)|(\.)|(e))+','match');
> r=sscanf(sprintf('%s ',r{:}),'%g')
> %{
> % r =
> 0
> -5.6e-005
> 1e-006
> %}
>
> us
Thanks a lot of the help guys. Works great.
|
|
0
|
|
|
|
Reply
|
Mike
|
7/30/2010 1:00:23 PM
|
|
|
10 Replies
776 Views
(page loaded in 0.199 seconds)
|