0
$\begingroup$

I need to read in a file with the first several lines reading as

SF,   2,   2,     0.0000000000000000000+I(     0.3141592653589793116),     0.0000000000000000000, -1000.0000000000000000000,     0.0000000000000000000+I(     0.0000000000000000000),   -1,
SF,   2,   0,     0.0000000000000000000+I(     0.3141592653589793116), -1000.0000000000000000000,  -333.3333333333333712289,     0.2746530255945797139+I(    -0.0001106096657636321),    1,
SF,   2,   2,     0.0000000000000000000+I(     0.3141592653589793116), -1000.0000000000000000000,  -333.3333333333333712289,     0.4718774055388476052+I(    -0.0003318288732658922),    1,

I need to read it into a list and then partition it into a matrix for subsequent treatment. My questions is how to read this mixed data set into a list? After it is read in, what type of data it is? It seems SF will not be stored as string, I need to use ToString to force it to be interpreted as string.

$\endgroup$
  • $\begingroup$ it seems there is no ready way to read in this mixed type data, is it? I used the following script to do it, can you take a look to see whether there are alternative simpler ways? $\endgroup$ – bsmile May 17 '15 at 23:05
  • $\begingroup$ Have you tried Import["D:\Code_Develop\data_mathematica.cvs", "Table"]? Tweak options as seen fit. $\endgroup$ – J. M. will be back soon May 18 '15 at 3:08
  • $\begingroup$ Many thanks! It seems Import (with format "CSV") does the same thing as OpenRead+ReadList+StringSplit in what I posted below. To transfer them to be usable forms like into numbers, strings etc, are there more compact ways to use to replace the role of two For loops in my post? $\endgroup$ – bsmile May 18 '15 at 5:31
  • $\begingroup$ Supposedly, anything that's a number in the file is supposed to come out as a number in Mathematica, and a string otherwise. Did you see any unexpected behavior? If you want to check the list produced, try applying InputForm[]. $\endgroup$ – J. M. will be back soon May 18 '15 at 5:35
  • $\begingroup$ I think it reads everything as strings. I checked with a presumably number multiplied with 2, and the output is not equal to a number multiplying 2. The default behavior might depend on what the type of the first entry Mathematica determines it to be, which I am not sure. $\endgroup$ – bsmile May 18 '15 at 20:27
1
$\begingroup$

It seems there is no ready way to read in this mixed type data. I used the following script to do it, can you take a look to help improve it? The code itself has been tested working, but not quite in the flow of mathematica.

 f = OpenRead["D:\Code_Develop\data_mathematica.cvs"];
 cc = ReadList[f, String];
 data1 = StringSplit[cc, ","]; ccm = data1;
 nn = Dimensions[ccm]; nn1 = nn[[1]];
 For[i = 1, i ≤ nn1, i++,
     ccm[[i, 1]] = StringTake[data1[[i, 1]], -2];
     For[j = 2, j ≤ 8, j++,
       ccm[[i, j]] = ToExpression[data1[[i, j]]];
       ]
     ];
 Close[f];
$\endgroup$
1
$\begingroup$

As suggested by @guess I would use Import[..., "Table"] to read in the data as a mixture of strings and numeric values, then use a rule to convert the complex values (with not a For loop in sight!). This method does not require you to know the complex column positions in advance.

Firstly I copied and pasted your example data to create a string:

stringData = 
  "SF,   2,   2,     0.0000000000000000000+I(     \
0.3141592653589793116),     0.0000000000000000000, \
-1000.0000000000000000000,     0.0000000000000000000+I(     \
0.0000000000000000000),   -1,
SF,   2,   0,     0.0000000000000000000+I(     \
0.3141592653589793116), -1000.0000000000000000000,  \
-333.3333333333333712289,     0.2746530255945797139+I(    \
-0.0001106096657636321),    1,
SF,   2,   2,     0.0000000000000000000+I(     \
0.3141592653589793116), -1000.0000000000000000000,  \
-333.3333333333333712289,     0.4718774055388476052+I(    \
-0.0003318288732658922),    1,";

Then I have to use the equivalent of Import for strings, ImportString.

ImportString[stringData, "Table", "FieldSeparators" -> ","] /. 
 s_String /; StringContainsQ[s, "+I("] :> ToExpression[s]

{{"SF", 2, 2, 0.*10^-19 + 0.314159265358979312 I, 0., -1000.000000000000000000, 0.*10^-19 + 0.*10^-19 I, -1}, {"SF", 2, 0, 0.*10^-19 + 0.314159265358979312 I, -1000.000000000000000000, -333.333333333333371229, 0.274653 - 0.00011061 I, 1}, {"SF", 2, 2, 0.*10^-19 + 0.314159265358979312 I, -1000.000000000000000000, -333.333333333333371229, 0.471877 - 0.000331829 I, 1}}

Obviously, to read from a file use Import["D:\\Code_Develop\\data_mathematica.cvs", ... instead and if you are using a version of Mathematica prior to 10.1 use StringMatchQ[s, __ ~~ "+I(" ~~ __] instead of StringContainsQ[...].

$\endgroup$

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.