开发者

Why does Maple fail when reading Comma/Space Separated Values and how to fix it?

I am trying to parse data such as:

29 28 23 19 14 11 13 17 
36 26 21 21 13 8 7 6 
54 33 25 26 18 13 10 3 
70 45 29 26 20 19 16 6 
85 63 41 27 22 23 20 13 
94 82 58 36 27 25 20 18 
93 91 76 53 36 26 21 18 
94 96 90 71 47 32 26 26 

1.60721 -0.529301 6.88206 5.14482 

27 30 32 34 37 40 39 36 
6 8 10 10 12 13 14 13 
2 1 2 3 4 5 7 10 
3 1 2 3 5 6 7 12 
5 1 1 3 4 4 7 10 
11 6 3 3 5 6 8 10 
12 9 4 3 5 8 11 14 
16 14 11 10 13 18 24 28 

0.709391 6.50125 0.745197 0.4955 

39 38 37 39 40 40 40 40 
20 21 21 22 23 23 24 24 
14 16 17 17 16 17 18 18 
12 12 13 12 12 13 13 14 
12 12 12 12 12 12 11 10 
13 11 11 13 14 14 13 13 
17 16 17 20 22 21 21 22 
34 33 34 39 40 38 40 44 

6.36007 0.492539 6.03537 6.58187 

With an algorithm such as:

开发者_运维问答 restart:
Z:="C://TEMP//mydata.txt":

fclose(Z);

#M:=Array(1..100):
#V:=Array(1..100):

for i from 1 to 100 do
   try
      M[i]:=fscanf(Z,"%{8,8}ldm")[1];
      V[i]:=fscanf(Z,"%{4}ldr")[1];
   catch "end of input encountered":
      break;
   end try;
end do;

M[2]; # returns the 2nd entry (a 8x8 Matrix) of M
V[2]; # returns the 2nd entry (a 1x4 row Vector) of V

but it fails with Error, (in fscanf) invalid character . encountered while reading Vector

How can I fix such an error ?


In the line of code,

V[i]:=fscanf(Z,"%{4}ldr")[1];

the format modifier "ldr" means ld (long integer) values going into a row (r) Vector. You got an error because the data contains floats, and that code was written to expect integers specifically. (Your previous post on this topic had all integer-valued data.)

If you now have a text file with some floats in it, then you could change the that code line for handling the Vector data to any of these,

V[i]:=fscanf(Z,"%{4}fr")[1];

or

V[i]:=fscanf(Z,"%{4}er")[1];

or

V[i]:=fscanf(Z,"%{4}gr")[1];

since %g, %e, and %f are float format modifiers for scanf.

If you don't know what sort of entries are in the file, you could also try,

V[i]:=fscanf(Z,"%{4}ar")[1];

since in Maple's scanf the %a descriptor means "algebraic" (which includes integers and floats, amongst many other types).

See the help-page ?scanf in Maple's own help system or online at,

scanf

acer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜