2
$\begingroup$

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!

$\endgroup$

2 Answers 2

5
$\begingroup$

Change this

 {x_, y_} -> x + I  y

to

{x_?NumberQ, y_?NumberQ} -> x + I  y
$\endgroup$
4
$\begingroup$

It may be those newlines that are messing with you. How about a different approach?

   data = Import["imag-data.txt", "Table"]~Select~((# != {}) &)
    ({#1, I #2} & @@@ Drop[data, 1])~Partition~2

    (*
   {{{0.933975, 0. + 2.39067*10^-16 I}, {-0.966082, 
   0. - 3.66569*10^-16 I}}, {{1.31581, 
   0. - 3.72881*10^-16 I}, {0.0535199, 
   0. - 4.98335*10^-17 I}}, {{0.447296, 
   0. + 2.92464*10^-17 I}, {0.607543, 
   0. - 5.46844*10^-17 I}}, {{-0.449118, 
   0. + 1.31663*10^-16 I}, {0.143307, 
   0. + 1.82559*10^-16 I}}, {{0.146835, 
   0. + 3.82662*10^-16 I}, {-0.551021, 
   0. + 9.12763*10^-18 I}}, {{-5.77383*10^-16, 
   0. + 1.01812*10^-17 I}, {-5.09058*10^-18, 
   0. - 2.88691*10^-16 I}}, {{0.0297388, 
   0. + 2.11297*10^-16 I}, {-0.202085, 0. + 4.59092*10^-18 I}}}
    *)

Maybe you need a different partition but could be a simpler and more robust way of getting to the data. Those 0.+a*I are a little annoying.

$\endgroup$
1
  • $\begingroup$ Complex@@@column-data is what you need to avoid 0.+a I $\endgroup$
    – luyuwuli
    May 12, 2013 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.