Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mfscanf doesn't work well

144 views
Skip to first unread message

friedrich...@gmail.com

unread,
Aug 4, 2008, 4:36:58 AM8/4/08
to
Hello guys!

I have a short, but time-eating problem. I have to read out a text-
file with 3 columns. The problem is that first column has a lot of
positions after the decimal point and mfscanf doesn't read in
correctly:

For example:

-3.2428714E-03,-3.06E-01,-9.0E-03
-3.2428614E-03,-2.99E-01,-7.4E-03
-3.2428514E-03,-3.19E-01,-5.0E-03
-3.2428414E-03,-3.34E-01,-4.8E-03
-3.2428314E-03,-3.37E-01,-1.21E-02
-3.2428214E-03,-3.04E-01,-2.44E-02

I read in this file with this code:

fd = mopen('Test.txt','r');
[n,a,b,c] = mfscanf(-1,fd, ' %f,%f,%f ' );
mclose(fd);

The result of the first column:

-->a
a =
- 0.0032429
- 0.0032429
- 0.0032429
- 0.0032428
- 0.0032428
- 0.0032428

-->a(1) + 3.2428714E-03
ans =
- 9.124D-11

As you can see, the values are wrong.

Can anybody help me???

Best regards,
Fritz

Helmut Jarausch

unread,
Aug 4, 2008, 6:06:57 AM8/4/08
to friedrich...@gmail.com
friedrich...@gmail.com wrote:
> Hello guys!
>
> I have a short, but time-eating problem. I have to read out a text-
> file with 3 columns. The problem is that first column has a lot of
> positions after the decimal point and mfscanf doesn't read in
> correctly:
>
> For example:
>
> -3.2428714E-03,-3.06E-01,-9.0E-03
> -3.2428614E-03,-2.99E-01,-7.4E-03
> -3.2428514E-03,-3.19E-01,-5.0E-03
> -3.2428414E-03,-3.34E-01,-4.8E-03
> -3.2428314E-03,-3.37E-01,-1.21E-02
> -3.2428214E-03,-3.04E-01,-2.44E-02
>
> I read in this file with this code:
>
> fd = mopen('Test.txt','r');
> [n,a,b,c] = mfscanf(-1,fd, ' %f,%f,%f ' );
> mclose(fd);
>

please do

help format

and then

format(16)
a

> The result of the first column:
>
> -->a
> a =
> - 0.0032429
> - 0.0032429
> - 0.0032429
> - 0.0032428
> - 0.0032428
> - 0.0032428
>
> -->a(1) + 3.2428714E-03
> ans =
> - 9.124D-11
>
> As you can see, the values are wrong.
>
> Can anybody help me???
>
> Best regards,
> Fritz


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

Francis

unread,
Aug 4, 2008, 6:14:13 AM8/4/08
to

You know that -9.12E-11 is a order lower than the your number which
has an accuracy of 1E-10. Perhaps you could set the values of the
matrix to zero before you fill them with your number. Not sure this
will work.

Francis

friedrich...@gmail.com

unread,
Aug 4, 2008, 6:27:49 AM8/4/08
to
> please do
>
> help format
>
> and then
>
> format(16)
> a

Was a good hint, but didn't really solve my problem:

-->format(16)
-->a
a =
- 0.0032428714912
- 0.0032428614795
- 0.0032428514678
- 0.0032428414561
- 0.0032428314444
- 0.0032428214327

Does scilab fill up the last digits with random numbers?? In my file
are 3 digits less than I got here!?!

My problem is, that I have not just 6 lines, I have 1.000.000 lines
(35MB file) and the first column is the time stored by the agilent
oscilloscope. So there should be 1e-8 steps between each value.

Thanks for your help!
Fritz

Enrico Segre

unread,
Aug 4, 2008, 8:06:19 AM8/4/08
to

Helmut Sennewald

unread,
Aug 4, 2008, 8:11:51 AM8/4/08
to

<friedrich...@gmail.com> schrieb im Newsbeitrag
news:780c4add-e2fc-4abe...@k37g2000hsf.googlegroups.com...

Hallo Fritz,

What's about this solution with read()?

--> format('e',14)

--> a=read('Test.txt',-1,3)

a =

- 3.2428714D-03 - 3.0600000D-01 - 9.0000000D-03
- 3.2428614D-03 - 2.9900000D-01 - 7.4000000D-03
- 3.2428514D-03 - 3.1900000D-01 - 5.0000000D-03
- 3.2428414D-03 - 3.3400000D-01 - 4.8000000D-03
- 3.2428314D-03 - 3.3700000D-01 - 1.2100000D-02
- 3.2428214D-03 - 3.0400000D-01 - 2.4400000D-02


Helmut


jpc

unread,
Aug 4, 2008, 12:26:58 PM8/4/08
to
On Aug 4, 10:36 am, friedrich.wiesin...@gmail.com wrote:
> Hello guys!
>
> I read in this file with this code:
>
> fd = mopen('Test.txt','r');
> [n,a,b,c] = mfscanf(-1,fd, ' %f,%f,%f ' );
> mclose(fd);
>

Hi,
The value you read are correct since you are reading in simple
precision (float)
you would have the same result with the fscanf performed at C level
If you want double precision you have to use %lf and not %f
[n,a,b,c] = mfscanf(-1,fd, ' %lf,%lf,%lf ' );
...
format(16)
-->a
a =

- 0.0032428714
- 0.0032428614
- 0.0032428514
- 0.0032428414
- 0.0032428314
- 0.0032428214

Since scilab only use double precision internally %lf could be
performed
internally when a user specify %f.
jpc

friedrich...@gmail.com

unread,
Aug 5, 2008, 11:50:01 AM8/5/08
to
> Hi,
> The value you read are correct since you are reading in simple
> precision (float)
> you would have the same result with the fscanf performed at C level
> If you want double precision you have to use %lf and not %f
> [n,a,b,c] = mfscanf(-1,fd, ' %lf,%lf,%lf ' );
> ...
> format(16)
> -->a
>  a  =
>
>   - 0.0032428714
>   - 0.0032428614
>   - 0.0032428514
>   - 0.0032428414
>   - 0.0032428314
>   - 0.0032428214
>
> Since scilab only use double precision internally %lf could be
> performed
> internally when a user specify %f.
> jpc

Hi guys!

Thanks a lot for your solutions!!!!!

Best regards,
Fritz

0 new messages