1

I'm trying to import some data in a .txt file which contains numbers and words. I import a list which consists of four rows of measured data acquired with an oscilloscope, but the acquisition program puts units in every measured value. Here's an example:

data={{-1.1000u s,-120.00m V,-1.1000u s,8.0000 V},
      {-1.0800u s,-120.00m V,-1.0800u s,8.0000 V},
      {-60.000n s,-40.000m V,-60.000n s,8.0000 V},
      {-40.000n s,-40.000m V,-40.000n s,8.0000 V},
      {-20.000n s,-32.000m V,-20.000n s,8.0000 V},
      {0.0000 s,-32.000m V,0.0000 s,8.0000 V}}

I would like to know if there's any way I can turn the above list into something more like this:

newdata={{-1.1000,-0.12000,-1.1000,8.0000},
         {-0.060000,-0.040000,-0.060000,8.0000},
         {-0.040000,-0.040.000,-0.040000,8.0000},
         {-0.020000,-0.032000,-0.020.000,8.0000},
         {0.0000,-0.032000,0.0000,8.0000}}

I'll appreciate any ideas. Thanks.

Juan
  • 327
  • 1
  • 2
  • 9

3 Answers3

4

Your data consist of lists of lists, each of which contains strings of the form:

list = {"24.940u s", "-72.000m V", "24.940u s", "-8.0000 V"};

To convert it to the format you desire, you need to:

  • Identify the numeric portion in the string and convert it to a number
  • Grab the conversion units (u, m, n, etc.) and apply a suitable rule.

The following function does that (it should be easy to follow what it does based on the above points).

convertList[l : {__String}] := 
    With[{rules = {"u" -> 1, "m" -> 10^-3, "n" -> 10^-3, " " -> 1}},
        StringCases[l, x : NumberString ~~ s : WordCharacter | WhitespaceCharacter :> 
            ToExpression@x (s /. rules)]] // Flatten

convertList[list]
(* {24.94, -0.072, 24.94, -8.} *)

Now to do this on the entire data set that you've provided, you have to map it across all the lists (or make it Listable) as:

data = Import["/path/to/the/file.txt", "Data"];
convertList /@ data
rm -rf
  • 88,781
  • 21
  • 293
  • 472
2

Also

ToExpression /@  StringReplace[ToString /@ #, {"u" -> " 1", "m" | "n" -> " .001"}] & /@ 
                                  Import["c:\\yourfile", {"Table"}][[All, {1, 3, 5, 7}]]


(* {{-25., -0.08, -25., -8.}, {-24.98, -0.08, -24.98, -8.}, ....}} *)
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
1

Something like :

Modify as appropriate the list of rules

unitRules = {m -> 10^-3, u -> 10^-6, n -> 10^-9};

data /. unitRules  /. Times[b___, x_?NumericQ, a___] -> x

(* {{-1.1*10^-6, -0.12, -1.1*10^-6, 8.}, 
    {-1.08*10^-6, -0.12, -1.08*10^-6, 8.}, 
    {-6.*10^-8, -0.04, -6.*10^-8, 8.}, 
    {-4.*10^-8, -0.04, -4.*10^-8, 8.}, 
    {-2.*10^-8, -0.032, -2.*10^-8, 8.}, 
    {0., -0.032, 0., 8.}} *)
b.gates.you.know.what
  • 20,103
  • 2
  • 43
  • 84