I would like to import values from a text file to run a notebook. The notebook works fine when I cut and paste in all the input values manually. Here is a working version. But when I import the same the values from a text file using the Import command I get vastly different outcomes (i.e. this mess).

To minimize possible errors I'm only trying to import the first of four requested input sections, so this post is only regarding the ratevector element. The other input values I cut & pasted by hand.

I cannot figure out why the solutions are different when I import the rate vector vs. cut and pasting the ratevector.

As background, the original notebook was written by Townsend et. al. 2012 and should only calculate 3 values: probability of resolution prcortree, probability of incorrect resolution princtree & the probability of a polytomy prpolytomy for a given dataset. Thank you for any help you can give! I hope I tagged this correctly.

  • $\begingroup$ how is this different from this: mathematica.stackexchange.com/questions/76807/… ? $\endgroup$
    – george2079
    Mar 9, 2015 at 16:46
  • $\begingroup$ I incorporated the answer from that post (and changed how I import my data) and I still get the wrong solution after running the notebook. $\endgroup$
    – PGilbert
    Mar 9, 2015 at 16:52
  • $\begingroup$ Can you do Import[filename,"Lines"] and post the first few lines? $\endgroup$
    – george2079
    Mar 9, 2015 at 16:53
  • $\begingroup$ In[1]:= ratevector = Import["~/uce1_rates_SMALL.txt", "Lines"] Out[1]= {"0.6951, 0, 0.6951, 1.4425, 0, 0, 1.5222, 0.6951, 4.3295, 4.3283"} $\endgroup$
    – PGilbert
    Mar 9, 2015 at 17:08
  • $\begingroup$ Or did you want the first few lines after I evaluate the notebook? 1-0.5 Erfc[(0.707107 (-0.5+3.46945*10^-18 2.71828^(-2563.11 0.6951, 0, 0.6951, 1.4425, 0, 0, 1.5222, 0.6951, 4.3295, 4.3283)+8.13152*10^-20 2.71828^(-2520.76 0.6951, 0, 0.6951, 1.4425, 0, 0, 1.5222, 0.6951, 4.3295, 4.3283)-4.66207*10^-18 2.71828^(-2492.22 0.6951, 0, 0.6951, 1.4425, 0, 0, 1.5222, 0.6951, 4.3295, 4.3283)- $\endgroup$
    – PGilbert
    Mar 9, 2015 at 17:35

1 Answer 1


You might fix the garbage you are getting with ToExpression:

      ratevector = Import[file, "CSV"][[1]] // ToExpression

I'd consider that a bit of a hack and prefer to understand why you are getting strings in the first place though.


I think I figured out whats going on -- It seems if your file contains tabs, ie.


that fools the "CSV" import into thinking the items are strings.

This might do the trick:

    Import[filename, "Table","FieldSeparators" -> {"\t",","," "}][[1]]
  • $\begingroup$ Wow. That hack worked impressively. Not sure what's going on under the hood, but if I can get the correct computation that's good enough for me at the moment. Many thanks& cheers! $\endgroup$
    – PGilbert
    Mar 9, 2015 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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