I want to read in a data file, test.txt, in Mathematica. The file has the following content:
7
9.339746404911912e-01 2.390665790330537e-16
-9.660816659011047e-01 -3.665687545173490e-16
1.315811450789676e+00 -3.728808440402881e-16
5.351989286996225e-02 -4.983348938581225e-17
4.472964563970432e-01 2.924635360279648e-17
6.075434861678513e-01 -5.468444707473763e-17
-4.491183862398205e-01 1.316628013212084e-16
1.433072380629653e-01 1.825593170558248e-16
1.468347618480511e-01 3.826623805151808e-16
-5.510211996466368e-01 9.127627039612340e-18
-5.773829817302568e-16 1.018116881684214e-17
-5.090584408421044e-18 -2.886914908651284e-16
2.973875045664112e-02 2.112972391320319e-16
-2.020849668577390e-01 4.590918574118308e-18
I want to first read the top most integer number which says how many groups of two complex numbers I have. In this case it is 7 groups each of 2 complex numbers. Then I want to take each of these groups, which contains two lines, and assign the first line to x[1]
and the second line to x[2]
. e.g., from the first group, I should have x[1]={9.339746404911912 * 10^(-01) + I 2.390665790330537* 10^(-16)}
and x[2] = {-9.660816659011047 * 10^(-01) + -3.665687545173490 * 10^(-16)}
.
So, I wrote the following code:
SetDirectory["C:\\your_dir_containing_the_data_file"];
Dim = 2;
streamread = OpenRead["test.txt"];
num = Read[streamread, Number];
Skip[streamread, String];
Print[num];
Do[
tmp[j] = {};
Do[
tmp[j] = Append[tmp[j], Read[streamread, {Number, Number}]],
{i, Dim}
];
tmp[j] = tmp[j] /. {x_, y_} -> x + I y,
(*Print[Chop[Equations/.Thread[Var-> soltmp[MainCounter]]]],*)
{j, num}
];
maintmp = Table[tmp[j], {j, num}];
Close[streamread];
But when I execute it, it gives maintmp
{{0.933975 - 0.966082 I,
2.39067*10^-16 - 3.66569*10^-16 I}, {1.31581 +
0.0535199 I, -3.72881*10^-16 - 4.98335*10^-17 I}, {0.447296 +
0.607543 I,
2.92464*10^-17 - 5.46844*10^-17 I}, {-0.449118 + 0.143307 I,
1.31663*10^-16 + 1.82559*10^-16 I}, {0.146835 - 0.551021 I,
3.82662*10^-16 + 9.12763*10^-18 I}, {-5.77383*10^-16 -
5.09058*10^-18 I,
1.01812*10^-17 - 2.88691*10^-16 I}, {0.0297388 - 0.202085 I,
2.11297*10^-16 + 4.59092*10^-18 I}}
In short, it screws up the real and imaginary parts of each number to one another.
Now, surprisingly, the problem occurs only if Dim=2
, i.e., if the groups are made of exactly 2
complex numbers. If they were made of 1
or 2+
, then everything is fine. I think Mathematica gets confused here at some point for Dim=2
. Do you guys know what's going wrong here?
Thanks!